2015-02-10 66 views
0

我在看这个例子获取时间四舍五入到最近的5分钟间隔只有几分钟转换为字符串

DateTime dt = new DateTime(1385577373517L, DateTimeZone.UTC); 
// Prints 2013-11-27T18:36:13.517Z 
System.out.println(dt); 

// Prints 2013-11-27T18:36:00.000Z (Floor rounded to a minute) 
System.out.println(dt.minuteOfDay().roundFloorCopy()); 

// Prints 2013-11-27T18:30:00.000Z (Rounded to custom minute Window) 
int windowMinutes = 10; 
System.out.println(
    dt.withMinuteOfHour((dt.getMinuteOfHour()/windowMinutes) * windowMinutes) 
     .minuteOfDay().roundFloorCopy() 
    ); 

我想要做的是提取物只是被四舍五入为最接近的10分钟间隔一分钟(比如说“30”)并将其转换为一个字符串,这样我就可以在其他地方作为输入了。

+0

最近的10分钟间隔* 36是40,你想最近的10分钟间隔*小于或等于实际的分钟? – mstbaum 2015-02-10 22:34:47

+0

30或40对我来说可以。 40实际上会很好 – Maalamaal 2015-02-10 22:37:04

回答

1

我猜,你可以微调您的四舍五入规则周围:

DateTime dt = new DateTime(1385577373517L, DateTimeZone.UTC); 
    // Prints 2013-11-27T18:36:13.517Z 
    System.out.println(dt); 

    // Prints 2013-11-27T18:36:00.000Z (Floor rounded to a minute) 
    System.out.println(dt.minuteOfDay().roundFloorCopy()); 

    // Prints 2013-11-27T18:30:00.000Z (Rounded to custom minute Window) 
    int windowMinutes = 10; 
    System.out.println(
     dt.withMinuteOfHour((dt.getMinuteOfHour()/windowMinutes) * windowMinutes).minuteOfDay().roundFloorCopy() 
    );   

    SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("m"); 
    String minute = DATE_FORMAT.format(dt.toDate()); 

    String minString = "" + ((int)Math.round(Integer.parseInt(minute)/10)) * 10; 

    System.out.println("Result: " + minString); 
相关问题