zoukankan      html  css  js  c++  java
  • java 时间操作

    记录一些时间常用操作

    public class DateCa {
        
        public static void main(String[] args) throws ParseException {
            
            /** 时间操作首推Calendar 格里高利历 **/
            Calendar ca =  Calendar.getInstance();
            //获取日期的单个属性:年,月,周,日,时,分
            System.out.println(ca.get(Calendar.YEAR));
            System.out.println(ca.get(Calendar.MONTH));//获取月份从0开始
            System.out.println(ca.get(Calendar.WEEK_OF_YEAR));
            System.out.println(ca.get(Calendar.DAY_OF_MONTH));
            System.out.println(ca.get(Calendar.HOUR_OF_DAY));
            System.out.println(ca.get(Calendar.MINUTE));
    
            //日期操作
            ca.add(Calendar.YEAR, 2);//退后两年
            System.out.println(ca.get(Calendar.YEAR));
            ca.add(Calendar.MONTH, 2);//退后两月
            System.out.println(ca.get(Calendar.MONTH));
            ca.add(Calendar.DAY_OF_MONTH, 10);//退后两天
            System.out.println(ca.get(Calendar.DAY_OF_MONTH));
            
            //直接设置所需时间
            ca.set(Calendar.YEAR,100);//将给定的日历字段设置为给定值。
            System.out.println(ca.get(Calendar.YEAR));
            ca.set(2018, 7, 12);
            System.out.println(ca.get(Calendar.YEAR));
            System.out.println(ca.get(Calendar.MONTH));
            System.out.println(ca.get(Calendar.DAY_OF_MONTH));
            
            //时间比较推荐
            System.out.println(ca.after(new Date()));
            System.out.println(ca.before(new Date()));
    
            //也可以获取指定时区的时间系统
            System.out.println(Calendar.getInstance(Locale.ENGLISH));
            
            /** 其他时间操作**/
            //当前秒数
            System.out.println(System.currentTimeMillis());
            System.out.println(new Date().getTime());
            //时间格式化
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            System.out.println(sf.format(ca.getTime()));
            System.out.println(sf.parse("2018-10-12 09:18:23").after(new Date()));
        }
    }
    Stay Hungry,Stay Foolish!
  • 相关阅读:
    前端资源
    WCF常见异常-The maximum string content length quota (8192) has been exceeded while reading XML data
    Asp.Net MVC路由调试工具-RouteDebugger
    Java中String 的equals 和==详解
    记一次高级java工程师职位的面试
    java中Class对象详解和类名.class, class.forName(), getClass()区别
    2014读书计划
    Javascript quiz
    CSS3 Flexbox布局那些事
    前端开发中的图片优化
  • 原文地址:https://www.cnblogs.com/stay-9527/p/4014498.html
Copyright © 2011-2022 走看看