记录一些时间常用操作
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())); } }