package com.atguigu;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
public class DemoDate {
public static void main(String[] args) {
Date date = new Date();//当前系统时间
System.out.println(date);
System.out.println(date.getYear());
System.out.println(date.toLocaleString());
long time = date.getTime();//返回该日期时间对象距离1970-1-1 0.0.0.0毫秒之间的毫秒值
System.out.println(time);
Date date1 = new Date(time);//把该毫秒值换算成日期时间对象
System.out.println(date1);
Calendar calendar = Calendar.getInstance();//获取一个Calendar对象
//System.out.println(calendar);
int i = calendar.get(Calendar.DATE);
System.out.println(i);
int i1 = calendar.get(Calendar.DAY_OF_YEAR);
System.out.println(i1);
// 2020/9/10
Date date2 = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
String format = simpleDateFormat.format(date2);//把日期对象格式化为字符串
System.out.println(format);
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy/mm/dd HH:mm:ss");
String format1 = simpleDateFormat1.format(date2);
System.out.println(format1);
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy年mm月dd日 HH小时mm分ss秒");
String format2 = simpleDateFormat2.format(date2);
System.out.println(format2);
LocalDate now = LocalDate.now();
System.out.println(now.getYear());
System.out.println(now);
ZonedDateTime now1 = ZonedDateTime.now();
ZonedDateTime now2 = ZonedDateTime.now(ZoneId.of("America/new_York"));
System.out.println(now2);
}
}
Date类
最新推荐文章于 2024-11-03 11:01:20 发布