1.日期介绍
在Java中 时间原点被设置为 本初子午线所在时区的 1970年1月1日的午夜。从原点开始,每天按照 86400秒向前或向回度量。Java中获取时间的API 有Date类,System.currentTimeMillis(),Timestamp,LocalDate,LocalTime,下边做一个介绍。
- 日期格式设置都采用 DateFormat 的子类 SimpleDateFormat 进行设置,如 new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”)
2. util下的 Date 类
- 获取时间为毫秒,可以按照自己需求的格式,转后后进行输出,用SimpleDateFormat() 类进行格式设置。
Date date = new Date();
long time = date.getTime();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
String resultTime = dateFormat.format(time);
System.out.println("util包下的Date类" + time);
System.out.println("util包下的Date类,format后的:" + resultTime);
Date parse = dateFormat.parse("2022-2-1 12:58:36");
System.out.println("parse 日期解析成ms : " + parse.getTime());
3. System 下的方法
//2. System 下的方法, // * @return the difference, measured in milliseconds, between // * the current time and midnight, January 1, 1970 UTC. // * @see java.util.Date // */ //毫秒
long timeMillis = System.currentTimeMillis();
long nanoTime = System.nanoTime();
System.out.println("System.currentTimeMillis()获取日期:"+ timeMillis);
String timeMillisFormat = dateFormat.format(timeMillis);
System.out.println("timeMillisFormat后的:" + timeMillisFormat);
System.out.println("nanoTime 纳秒:" + nanoTime);
4. Timestamp(属于sql包) 用于将util获取的时间转为 Sql下可以存储的时间,与java.sql.Date 类功能类似。前者显示年月日,时分秒,后者只显示年月日。
Timestamp timestamp = new Timestamp(new Date().getTime());
System.out.println("用于sql的timeStamp timestamp :" + timestamp);
java.sql.Date date1 = new java.sql.Date(new Date().getTime());
System.out.println("java.sql.Date : " + date1);
5. Calendar
Calendar calendar = new GregorianCalendar();
Date time2 = calendar.getTime();
System.out.println("Calendar中的time2 " + time2);
Instant now = Instant.now();
System.out.println("Instant " + now);
6.LocalDate & LocalTime(LocalDate 有一些方法getDayOfMonth();getDayOfWeek();getDayOfYear();LocalTime方法:getMinute();getHour();)
LocalDate now1 = LocalDate.now();
System.out.println("LocalDate " + now1);
int dayOfMonth = now1.getDayOfMonth();
System.out.println("dayOfMonth "+ dayOfMonth);
DayOfWeek dayOfWeek = now1.getDayOfWeek();
System.out.println("DayOfWeek " + dayOfWeek);
int dayOfYear = now1.getDayOfYear();
System.out.println("dayOfYear " + dayOfYear);
LocalTime now2 = LocalTime.now();
System.out.println("LocalTime " + now2);
int minute = now2.getMinute();
System.out.println("minute "+minute);
int hour = now2.getHour();
System.out.println("hour " + hour);
LocalDateTime now3 = LocalDateTime.now();
System.out.println("LocalDateTime "+now3);
7. 日期格式化第二种方法: DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = dateTimeFormatter.format(dateTime);
System.out.println("DateTimeFormatter :" + format);
TemporalAccessor parse1 = dateTimeFormatter.parse("2021-05-04 07:56:24");
System.out.println("TemporalAccessor " +parse1 );
全部代码展示
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class Day6_Date_Calendar_Time_TimeStamp {
public static void main(String[] args) throws ParseException {
Date date = new Date();
long time = date.getTime();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String resultTime = dateFormat.format(time);
System.out.println("util包下的Date类" + time);
System.out.println("util包下的Date类,format后的:" + resultTime);
Date parse = dateFormat.parse("2022-2-1 12:58:36");
System.out.println("parse 日期解析成ms : " + parse.getTime());
long timeMillis = System.currentTimeMillis();
long nanoTime = System.nanoTime();
System.out.println("System.currentTimeMillis()获取日期:"+ timeMillis);
String timeMillisFormat = dateFormat.format(timeMillis);
System.out.println("timeMillisFormat后的:" + timeMillisFormat);
System.out.println("nanoTime 纳秒:" + nanoTime);
Timestamp timestamp = new Timestamp(new Date().getTime());
System.out.println("用于sql的timeStamp timestamp :" + timestamp);
java.sql.Date date1 = new java.sql.Date(new Date().getTime());
System.out.println("java.sql.Date : " + date1);
Calendar calendar = new GregorianCalendar();
Date time2 = calendar.getTime();
System.out.println("Calendar中的time2 " + time2);
Instant now = Instant.now();
System.out.println("Instant " + now);
LocalDate now1 = LocalDate.now();
System.out.println("LocalDate " + now1);
int dayOfMonth = now1.getDayOfMonth();
System.out.println("dayOfMonth "+ dayOfMonth);
DayOfWeek dayOfWeek = now1.getDayOfWeek();
System.out.println("DayOfWeek " + dayOfWeek);
int dayOfYear = now1.getDayOfYear();
System.out.println("dayOfYear " + dayOfYear);
LocalTime now2 = LocalTime.now();
System.out.println("LocalTime " + now2);
int minute = now2.getMinute();
System.out.println("minute "+minute);
int hour = now2.getHour();
System.out.println("hour " + hour);
}
}
|