import java.util.Date;
/**
* Date
* 代表了一个特定的时间,精确到毫秒
*/
public class MyDate {
public static void main(String[] args) {
//空参,表示电脑中的当前时间
Date date = new Date();//Sun Jan 03 09:18:48 CST 2021
System.out.println(date);
//从计算机时间原点开始,过了指定毫秒的那个时间
Date date1 = new Date(0L);//Thu Jan 01 08:00:00 CST 1970
System.out.println(date1);
//把当前时间封装成一个对象
Date date2 = new Date();
//获取时间对象的毫秒值
long time = date2.getTime();
System.out.println(time);
System.out.println(System.currentTimeMillis());
//设置时间,传递毫秒值
date2.setTime(0L);
System.out.println(date2);
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* SimpleDateFormat
* 可以对 Date 对象,进行格式化和解析
*/
public class MySimpleDateFormat {
public static void main(String[] args) throws ParseException {
//日期对象
Date date = new Date();
//日期格式对象
SimpleDateFormat sdf =
new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//格式化
String format = sdf.format(date);
System.out.println(format);//2021年01月03日 09:59:25
//解析
String str = "2020-02-02";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf1.parse(str);
System.out.println(date1);//Sun Feb 02 00:00:00 CST 2020
}
}
package com.llb.date;
import java.time.LocalDateTime;
/**
* JDK1.8的时间工具类
*/
public class MyJDK8Date {
public static void main(String[] args){
LocalDateTime now =
LocalDateTime.of(2020, 12, 12, 12, 12, 12, 12);
System.out.println(now);
//获取年getYear()
int year = now.getYear();
System.out.println("year : "+year);
//获取月getMonthValue()
int monthValue = now.getMonthValue();
System.out.println("monthValue : "+monthValue);
//获取月份中的第几天getDayOfMonth()
int dayOfMonth = now.getDayOfMonth();
System.out.println("dayOfMonth : "+dayOfMonth);
System.out.println("dayOfWeek : "+now.getDayOfWeek());
System.out.println("dayOfYear : "+now.getDayOfYear());
System.out.println("hour : "+now.getHour());
System.out.println("minute : "+now.getMinute());
System.out.println("second : "+now.getSecond());
}
}
Date SimpleDateFormat
最新推荐文章于 2022-12-13 09:02:35 发布