博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java Date API demo
阅读量:7053 次
发布时间:2019-06-28

本文共 3887 字,大约阅读时间需要 12 分钟。

 

package date;import java.text.DateFormat;import java.util.Calendar;import java.util.Date;/*2015-9-9*/public class DateDemo {    /**     * @param args     */    public static void main(String[] args) {        Date now = new Date();        System.out.println("用Date方式显示时间: " + now);//此方法显示的结果和Calendar.getInstance().getTime()一样        DateFormat d1 = DateFormat.getDateInstance(); //默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-16 20:54:53)        String str1 = d1.format(now);        System.out.println("用DateFormat.getDateInstance()格式化时间后为:" + str1);        DateFormat d2 = DateFormat.getDateTimeInstance();        String str2 = d2.format(now);        System.out.println("用DateFormat.getDateTimeInstance()格式化时间后为:" + str2);        DateFormat d3 = DateFormat.getTimeInstance();        String str3 = d3.format(now);        System.out.println("用DateFormat.getTimeInstance()格式化时间后为:" + str3);        DateFormat d4 = DateFormat.getInstance(); //使用SHORT风格显示日期和时间        String str4 = d4.format(now);        System.out.println("用DateFormat.getInstance()格式化时间后为:" + str4);        DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL); //显示日期,周,时间(精确到秒)        String str5 = d5.format(now);        System.out.println("用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为:" + str5);        DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); //显示日期。时间(精确到秒)        String str6 = d6.format(now);        System.out.println("用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为:" + str6);        DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); //显示日期,时间(精确到分)        String str7 = d7.format(now);        System.out.println("用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后为:" + str7);        DateFormat d8 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); //显示日期,时间(精确到分)        String str8 = d8.format(now);//与SHORT风格相比,这种方式最好用        System.out.println("用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间后为:" + str8);        DateFormat d9=DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM);        String str9=d9.format(now);        System.out.println("DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM)"+str9);        Calendar ca = Calendar.getInstance();        int year = ca.get(Calendar.YEAR);//获取年份        int month = ca.get(Calendar.MONTH);//获取月份        int day = ca.get(Calendar.DATE);//获取日        int minute = ca.get(Calendar.MINUTE);//分        int hour = ca.get(Calendar.HOUR);//小时        int second = ca.get(Calendar.SECOND);//秒        int WeekOfYear = ca.get(Calendar.DAY_OF_WEEK);        System.out.println("用Calendar.getInstance().getTime()方式显示时间: " + ca.getTime());        System.out.println("用Calendar获得日期是:" + year + "年" + month + "月" + day + "日");        System.out.println("用Calendar获得时间是:" + hour + "时" + minute + "分" + second + "秒");        System.out.println(WeekOfYear);//显示今天是一周的第几天(我做的这个例子正好是周二,故结果显示2,如果你再周6运行,那么显示6)                    }}

输出:

用Date方式显示时间: Wed Sep 09 21:37:04 CST 2015用DateFormat.getDateInstance()格式化时间后为:2015-9-9用DateFormat.getDateTimeInstance()格式化时间后为:2015-9-9 21:37:04用DateFormat.getTimeInstance()格式化时间后为:21:37:04用DateFormat.getInstance()格式化时间后为:15-9-9 下午9:37用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为:2015年9月9日 星期三 下午09时37分04秒 CST用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为:2015年9月9日 下午09时37分04秒用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后为:15-9-9 下午9:37用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间后为:2015-9-9 21:37:04DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM)2015年9月9日 21:37:04用Calendar.getInstance().getTime()方式显示时间: Wed Sep 09 21:37:04 CST 2015用Calendar获得日期是:2015年8月9日用Calendar获得时间是:9时37分4秒4

 

转载地址:http://ubdol.baihongyu.com/

你可能感兴趣的文章
php+mysql 判断记录位置
查看>>
方法与函数的区别
查看>>
HLS-AES 128 格式视频服务,flash 通过 https 获取加密密钥失败原因以及解决方法
查看>>
linux常用软件安装命令
查看>>
wordpress功能集成(三):基础知识-wordpress过滤器
查看>>
mysql 笔记
查看>>
const
查看>>
女人为什么喜欢坏男人--无语贴
查看>>
合格程序员每天每周每月每年应该做的事
查看>>
macbook pro(2012款)更换ssd硬盘(光驱拆除换成ssd,原有硬盘仍然使用)
查看>>
Stream API(三)--原始类型流特化
查看>>
使用webiopi控制树莓派的GPIO引脚电平(WEB在线管理)
查看>>
js中call与apply
查看>>
隐式转换
查看>>
Java 8 - 20 Examples of Date and Time API Read mo
查看>>
JDK8新特性-方法引用
查看>>
(转)直接拿来用!最火的iOS开源项目(二)
查看>>
MikroTik RouterOS ™ V2.6 基本安装
查看>>
有状态、无状态(Stateful and Stateless)
查看>>
XA式、非XA式Spring分布式事务的实现
查看>>