java 中cal.get(Calendar.HOUR_OF_DAY) 取小时 如果是中午12点取值为0 我怎么样才能取到12

java 中cal.get(Calendar.HOUR_OF_DAY) 取小时 如果是中午12点取值为0 我怎么样才能取到12,第1张

calget(CalendarHOUR_OF_DAY) 取的就是24时钟数

calget(CalendarHOUR) 取的就是12时钟数

看一下它们的注释就清楚了。

/

Field number for <code>get</code> and <code>set</code> indicating the

hour of the morning or afternoon <code>HOUR</code> is used for the

12-hour clock (0 - 11) Noon and midnight are represented by 0, not by 12

Eg, at 10:04:15250 PM the <code>HOUR</code> is 10

@see #AM_PM

@see #HOUR_OF_DAY

/

public final static int HOUR = 10;

/

Field number for <code>get</code> and <code>set</code> indicating the

hour of the day <code>HOUR_OF_DAY</code> is used for the 24-hour clock

Eg, at 10:04:15250 PM the <code>HOUR_OF_DAY</code> is 22

@see #HOUR

/

public final static int HOUR_OF_DAY = 11;

如果使用的是calget(CalendarHOUR),可能通过calget(CalendarAM_PM)的返回值来判断是上午还是下午,如果返回0,则是上午,如果返回1,则是下午。判断的时候,最好用类定义的常量来比较。

if(CalendarAM==calget(CalendarAM_PM)){

//上午

}else /if(CalendarPM==calget(CalendarAM_PM))/{

//下午

}

Calendar ca = CalendargetInstance(); int year = caget(CalendarYEAR);//获取年份 int month=caget(CalendarMONTH);//获取月份 int day=caget(CalendarDATE);//获取日 int minute=caget(CalendarMINUTE);//分 int hour=caget(CalendarHOUR);//小时 int second=caget(CalendarSECOND);//秒 int WeekOfYear = caget(CalendarDAY_OF_WEEK);

Calendar c = CalendargetInstance();//可以对每个时间域单独修改

int year = cget(CalendarYEAR);

int month = cget(CalendarMONTH);

int date = cget(CalendarDATE);

int hour = cget(CalendarHOUR_OF_DAY);

int minute = cget(CalendarMINUTE);

int second = cget(CalendarSECOND);

//前一个小时的时间

Systemoutprintln(year + "/" + month + "/" + date + " " +(hour - 1)+ ":" +minute + ":" + second);

public class CalendarTest {

public static void main(String[] args) {

Calendar calendar = CalendargetInstance();

Systemoutprintln(calendarget(CalendarHOUR_OF_DAY));

}

}

我这边执行写的代码没有问题 ,楼主可以参考一下

以上就是关于java 中cal.get(Calendar.HOUR_OF_DAY) 取小时 如果是中午12点取值为0 我怎么样才能取到12 全部的内容,包括:java 中cal.get(Calendar.HOUR_OF_DAY) 取小时 如果是中午12点取值为0 我怎么样才能取到12 、java如何通过calendar类获取当前系统日期、Java 怎么获取当前时间前一个小时的时间等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9751749.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存