java日期操作

import java.text.SimpleDateFormat;
import java.util.Date;

//日期操作
public class TestDemo{
    public static void main(String args[]){

        //Date类在util包下 获取当前系统的时间
        Date time=new Date();//无参构造 表示当前时间
        System.out.println(time);//Wed May 06 22:19:13 CST 2020 调用toString方法,打印出特殊格式下的当前时间
        Date time1=new Date(1);//含参构造 参数为距离1970年1月1日0时0分的毫秒数 得到的是此日期


        // SimpleDateFormat类在text包下 格式化和解析日期的具体类。它允许进行格式化(日期 -> 文本)、解析(文本 -> 日期)和规范化。
        SimpleDateFormat tt1=new SimpleDateFormat("yyyy-MM-dd E HH时mm分ss秒SSS");//构造方法中规定了时间的格式
        String tt=tt1.format(time);//SimpleDateFormat类中的方法可以接受一个Date类,返回规定格式的字符串
        System.out.println(tt);//2020-05-06 星期三 22时26分42秒150
        SimpleDateFormat t=new SimpleDateFormat("yyyy-MM-dd E HH时mm分SSS秒");
        String str=t.format(time1);
        System.out.println(str);//1970-01-01 星期四 08时00分001秒 本地是东8区 差八个小时

        //lang包下的System类中的currentTimeMillis()方法可以返回当前时间到1970年1月1日的毫秒数,利用此方法我们可以计算程序运行时间
        long t1=System.currentTimeMillis();
        long t2=System.currentTimeMillis();
        System.out.println("程序运行耗时为:"+(t2-t1)+"毫秒");//程序运行耗时为:0毫秒

        Date tzuo =new Date(System.currentTimeMillis()-1000*60*60*24);//获取24小时前的时间
        SimpleDateFormat tzuo1=new SimpleDateFormat("yyyy-MM-dd E HH时-mm分-ss秒SSS");
        String tzuo2=tzuo1.format(tzuo);
        System.out.println(tzuo2); //2020-05-05 星期二 22时-46分-41秒260
    }

}
package 第十一届蓝桥杯;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main02 {


		public static void main(String[] args) throws ParseException {
			SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
			Date d1 =  s.parse("1921-7-23 12-00-00");
			Date d2 =  s.parse("2020-7-1 12-00-00");
			long t1 = d2.getTime()-d1.getTime();
			System.out.println(t1/60000);
		}
}

原文地址:https://www.cnblogs.com/fxzemmm/p/14847959.html