String、Date、LocalDateTime、Calendar与时间戳之间互相转化参考String、Date、LocalDateTime、Calendar与时间戳之间互相转化
package com.zyp.test.time;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* @author syl
* @description TODO
* @since 2022/5/28
*/
public class DateTest {
/**
* 格式化对象实例
*/
private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 格式化对象实例
*/
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/**
* date与字符串的互转
* @throws ParseException
*/
@Test
public void StringToDate() throws ParseException {
//把date转成指定格式的字符串
String nowString = SIMPLE_DATE_FORMAT.format(new Date());
System.out.println("nowString:"+nowString);
//把指定格式的字符串转成date
Date nowDate = SIMPLE_DATE_FORMAT.parse(nowString);
System.out.println("nowDate:"+nowDate);
//122 ,结果不符合预期,不建议使用date获取年月日时分秒
System.out.println("nowDate.getYear() = " + nowDate.getYear());
}
/**
* date与calender互转
*/
@Test
public void dateToCalendar(){
//获取Calendar实例,默认是当前时间
Calendar calendar = Calendar.getInstance();
//date转成calendar
calendar.setTime(new Date());
System.out.println(calendar.toString());
//calendar转成date
Date now = calendar.getTime();
System.out.println(now);
}
/**
* String与Calendar 此处借助中间类date
*/
@Test
public void stringToCalendar() throws ParseException {
Calendar calendar=Calendar.getInstance();
String dateString ="2018-12-08 12:00:08";
//String--->Calendar
calendar.setTime(SIMPLE_DATE_FORMAT.parse(dateString));
//Calendar--->String
String date1 = SIMPLE_DATE_FORMAT.format(calendar.getTime());
}
/**
* String与LocalDateTime
*/
@Test
public void stringToLocalDateTime(){
String dateString ="2018-12-08 12:00:08";
// String转成LocalDateTime
LocalDateTime localDateTime = LocalDateTime.parse(dateString, DATE_TIME_FORMATTER);
// LocalDateTime转成String
String date = localDateTime.format(DATE_TIME_FORMATTER);
}
/**
* date与LocalDateTime
*/
@Test
public void dateToLocalDateTime() throws ParseException {
String dateString ="2018-12-08 12:00:08";
Date date = SIMPLE_DATE_FORMAT.parse(dateString);
//date转成LocalDateTime
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
//LocalDateTime转成date
Date date1 = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
System.out.println(SIMPLE_DATE_FORMAT.format(date1));
}
/**
* Calendar与LocalDateTime
* @throws ParseException
*/
@Test
public void calendarToLocalDateTime() throws ParseException {
Calendar calendar = Calendar.getInstance();
String dateString ="2018-12-08 12:00:08";
Date date = SIMPLE_DATE_FORMAT.parse(dateString);
calendar.setTime(date);
//Calendar转成LocalDateTime
LocalDateTime localDateTime = LocalDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault());
//LocalDateTime转成Calendar
Calendar calendar1 = GregorianCalendar.from(localDateTime.atZone(ZoneId.systemDefault()));
System.out.println("SIMPLE_DATE_FORMAT.format(calendar1.getTime()) = " + SIMPLE_DATE_FORMAT.format(calendar1.getTime()));
}
/**
* 时间戳与Date、Calendar、LocalDateTime
*/
@Test
public void test(){
long l = System.currentTimeMillis();
System.out.println(l);
Date date = new Date();
date.setTime(l);
date.getTime();
System.out.println("SIMPLE_DATE_FORMAT.format(date) = " + SIMPLE_DATE_FORMAT.format(date));
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(l);
calendar.getTimeInMillis();
System.out.println("SIMPLE_DATE_FORMAT.format(calendar.getTime()) = " + SIMPLE_DATE_FORMAT.format(calendar.getTime()));
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(l), ZoneId.systemDefault());
System.out.println("localDateTime.atZone(ZoneId.systemDefault()).toEpochSecond() = " + localDateTime.atZone(ZoneId.systemDefault()).toEpochSecond());
System.out.println("localDateTime.format(DATE_TIME_FORMATTER) = " + localDateTime.format(DATE_TIME_FORMATTER));
}
}