package com.cyjt97.dt;
import java.util.Date;
public class day {
public static void main(String[] args) {
Date dt = new Date();
Date dt2 = new Date(0L);
System.out.println(dt);//Sat Mar 18 16:09:15 CST 2023
System.out.println(dt2);//Thu Jan 01 08:00:00 CST 1970
System.out.println(dt.getTime());//1679127078299
System.out.println(dt2.getTime());//0
}
}
package com.cyjt97.dt; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class day2 { public static void main(String[] args) throws ParseException { // 创建日期格式化对象,封装时间 SimpleDateFormat simpleDateFormat = new SimpleDateFormat(); // 手动修改 SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); // 创建date对象,封装时间 Date date = new Date(); // 将日期时间转为字符串 String res =simpleDateFormat.format(date); String res2 =simpleDateFormat2.format(date); System.out.println(res);//2023/3/18 下午4:58 System.out.println(res2);//2023年03月18日 17:41:01 String today = "2023年3月18日"; SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy年MM月dd日"); Date date2 = simpleDateFormat1.parse(today); System.out.println(date2);//Sat Mar 18 00:00:00 CST 2023 } }
package com.cyjt97.dt; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class day3 { public static void main(String[] args) throws ParseException { Scanner sc = new Scanner(System.in); System.out.println("请输入您的生日"); String b = sc.nextLine(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日"); Date bb = simpleDateFormat.parse(b); Date TODAY = new Date(); long time = TODAY.getTime()-bb.getTime(); System.out.println(time/1000/60/60/24); } }
标签:java,进阶,System,SimpleDateFormat,println,Date,new,JDK7 From: https://www.cnblogs.com/wencaiguagua/p/17231003.html