Date、SimpleDateFormat日期类和Calender类

Date date = new Date();
        System.out.println(date);
        //返回自 19701100:00:00 GMT 以来此 Date 对象表示的毫秒数。
        System.out.println(date.getTime());
        Long d = 1486539701857l;
        //date.setTime(d);   //表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点。
        System.out.println(date);
        //格式化时间,注意DateFormat是一个抽象类,并且有些方法是静态的
        DateFormat df1= null;
        DateFormat df2=null;
        DateFormat df3=null;

        df1=DateFormat.getDateInstance();//获取日期格式器,该格式器具有默认语言环境的默认格式化风格
        df3=DateFormat.getInstance();
        df2=DateFormat.getDateTimeInstance();
        //格式化日期和时间输出
        System.out.println("Date:"+df1.format(date));//Date:2017-2-8
        System.out.println("Time:"+df3.format(date));//Time:17-2-8 下午4:01
        System.out.println("TimeDate:"+df2.format(date));//TimeDate:2017-2-8 16:27:07
        /**
         * static DateFormat: getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale) 
                               获取日期/时间格式器,该格式器具有给定语言环境的给定格式化风格。 
         */
        df1=DateFormat.getDateInstance(DateFormat.FULL,new Locale("zh", "CN"));
        System.out.println("中国人喜欢的格式:"+df1.format(date));//:2017年2月8日 星期三
        df2=DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,new Locale("zh", "CN"));
        System.out.println(df2.format(date));//2017年2月8日 星期三 下午04时27分07秒 CST

        //SimpleDateFormat  可以自己定义时间格式
        String str = "2017-03-19 17:18:30";
        Date date2 = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH-mm-ss");
        try {
            //Date java.text.DateFormat.parse(String source) throws ParseException
            date2=sdf.parse(str);
        } catch (Exception e) {
            // TODO: handle exception
        }
        System.out.println(date2);

        String string=sdf1.format(date2);
        System.out.println(string);

        Calendar calendar= Calendar.getInstance();
        System.out.println(calendar);
        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(Calendar.MONTH)+1);
        System.out.println(calendar.get(Calendar.DATE));
        System.out.println(calendar.get(Calendar.HOUR));
        System.out.println(calendar.get(Calendar.MINUTE));
        System.out.println(calendar.get(Calendar.SECOND));

        calendar.set(Calendar.YEAR, 2018);
        System.out.println(calendar.get(Calendar.YEAR));

        // 或者另一種設置 calendar 方式  
        // 分別爲 year, month, date, hourOfDay, minute, second  
        calendar = Calendar.getInstance();  
        calendar.set(2013, 1, 2, 17, 35, 44);  
        str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());  
        System.out.println(str);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值