获取给定时间范围内的当前时间

时间:2016-09-28 07:11:25

标签: android

所以我有一个时间范围的例子:

2016-09-26T12:00:00.000+08:00 / 2016-09-27T11:59:00.000+08:00

假设当前时间为2016-09-29T06:00:00.000+08:00

所以我想要的是在06:00 AM的上述时间范围内(2016-09-26或2016-09-27)落下哪个日期......

我尝试使用joda的Interval类,但我无法弄清楚如何使它工作。任何建议都表示赞赏。

预期产出样本:

  • 06:00 AM => 2016年9月27日
  • 06:00 PM => 2016年9月26日
  • 03:00 AM => 2016年9月27日
  • 03:00 PM => 2016年9月26日

2 个答案:

答案 0 :(得分:0)

    SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yy HH:mm:ss");

    String strEndDate = "2016-09-27T11:59:00.000+0800";
    SimpleDateFormat fmt = new SimpleDateFormat(
            "yyyy-MM-dd'T'HH:MM:ss.SSSZ");

    Date endDate = null;

    try {
        endDate = fmt.parse(strEndDate);
    } catch (Exception e) {
        // TODO: handle exception
    }

    Date currDate = new Date();  // you can set your date and time here

    while(currDate.after(endDate)){
        currDate.setTime(currDate.getTime()-(24*60*60*1000));
    }

    String result = fmt.format(currDate);

答案 1 :(得分:0)

我想我发现了一种适合我的方式

$ seq 10 | bash myscript
$ cat out1
1
10
$ cat out2
2
$ cat out3
3

我不确定它是否有效但它能满足我的需求。

相关问题