当前位置 : 主页 > 网络编程 > 其它编程 >

Java中时间类使用方法总结

来源:互联网 收集:自由互联 发布时间:2023-07-02
java.util.Date;Date类用法DatedatenewDate();System.out.println(date);System.out.println(; //java.util.Date; Date类用法Date date = new Date();System.out.println(date);System.out.println("=============================");//java.util.Calend
java.util.Date;Date类用法DatedatenewDate();System.out.println(date);System.out.println(; //java.util.Date; Date类用法Date date = new Date();System.out.println(date);System.out.println("=============================");//java.util.Calendar; Calendar类用法Calendar calendar = Calendar.getInstance();calendar.setTime(new Date());System.out.println(calendar.get(Calendar.YEAR) + "年" + calendar.get(Calendar.MONTH) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日");System.out.println("=============================");//java.util.GregorianCalendar; GregorianCalendar类用法GregorianCalendar gcall1 = new GregorianCalendar(2011,5,1);GregorianCalendar gcall2 = new GregorianCalendar(Locale.CHINESE);System.out.println("gcall1:" + gcall1.get(Calendar.YEAR) + " " + gcall1.get(Calendar.MONTH) + " " + gcall1.get(Calendar.DAY_OF_MONTH));System.out.println("gcall12:" + gcall2.get(Calendar.YEAR) + " " + gcall2.get(Calendar.MONTH) + " " + gcall2.get(Calendar.DAY_OF_MONTH));System.out.println("=============================");//java.text.DateFormat; DateFormat类的用法DateFormat format = DateFormat.getInstance();System.out.println(format.format(new Date()));System.out.println("=============================");//DateFormat中还定义了多个getDateInstance方法,用来格式化日期信息,如getDateInstance(int style, Locale alocale),可以指定格式化的风格以及Local信息DateFormat format2 = DateFormat.getDateInstance(DateFormat.SHORT,Locale.US);DateFormat format3 =DateFormat.getDateInstance(DateFormat.LONG,Locale.CHINESE);DateFormat format4 = DateFormat.getDateInstance(DateFormat.FULL,Locale.FRANCE);DateFormat format5 = DateFormat.getDateInstance(DateFormat.MEDIUM,Locale.JAPAN);System.out.println(format2.format(new Date()));System.out.println(format3.format(new Date()));System.out.println(format4.format(new Date()));System.out.println(format5.format(new Date()));System.out.println("=============================");//SimpleDateFormat类是DateFormat类的子类,可是使用任何用户定义的日期时间格式的模式进行格式化。SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日hh时mm分ss秒EE",Locale.CHINA);System.out.println(sdf1.format(new Date()));【本文由:阿里云代理 http://www.56aliyun.com欢迎留下您的宝贵建议】
上一篇:将汉字转成拼音
下一篇:没有了
网友评论