日期时间不等15分钟

时间:2013-02-13 02:52:11

标签: java jpa

我想检查当前日期时间恰好是15分钟

实施例:  10:15,10:30,10:45,11:00,11:15,11:30,11:45 ....

我怎样才能实现这个目标

Calendar calendar = Calendar.getInstance();

int hours = calendar.get(Calendar.HOUR_OF_DAY);

int minutes = calendar.get(Calendar.MINUTE);

int seconds = calendar.get(Calendar.SECOND);

int s=minutes %15;

if((minutes == 0 ) && s==0 )
{
    System.out.print("Time ");   

}
我可以像这样使用..

4 个答案:

答案 0 :(得分:4)

试试这个:

Calendar cal = Calendar.getInstance();
int minute = cal.get(Calendar.MINUTE);
if(minute % 15 == 0) 
{
    System.out.println("the current date time is exactly 15 minute");
}

答案 1 :(得分:2)

查看Quartz

CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                       .withSchedule(CronScheduleBuilder.cronSchedule("* 0/15 * * * ?"))
                       .build();

答案 2 :(得分:1)

Calendar.get(Calendar.MINUTE)可以帮助你。

public void minuteOfHour(){

    Calendar rightNow = Calendar.getInstance();
    System.out.println(rightNow.get(Calendar.MINUTE));
}

或者甚至更好,考虑Joda Time的DateTime API。它提供了方便的minuteOfHour方法。

http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html#minuteOfHour()

答案 3 :(得分:1)

public boolean isFifteenMin(DateTime datetime)
    return (Integer.parseInt(new SimpleDateFormat("m").format(datetime))%15==0) ? true :     false;
}