package com.xt.java2;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import static java.util.Calendar.DAY_OF_MONTH;
public class DateTimeTest {
@Test
public void testSimpleDateFormat() throws ParseException {
//实例化,使用默认的构造器
SimpleDateFormat sdf = new SimpleDateFormat();
//格式化:日期-->字符串
Date date = new Date();
System.out.println(date);
String s = sdf.format(date);
System.out.println(s);
//解析:字符串-->日期
String str="2022/4/19 上午10:22";
Date date1=sdf.parse(str);
System.out.println(date1);
}
@Test
public void testSimpleDateFormat1() throws ParseException {
//按照指定的方式格式化和解析:调用带参的构造器
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//格式化:日期-->字符串
Date date = new Date();
String str = sdf.format(date);
System.out.println(str+"格式化");
//解析:字符串-->日期 要求:字符串必须是SimpleDateFormat识别的格式(通过构造器参数体现)
Date date1 = sdf.parse("2022-04-19 10:42:11");
System.out.println(date1+"解析");
}
@Test
public void testExer() throws ParseException {
String birth="1997-06-20";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(birth);
java.sql.Date date1=new java.sql.Date(date.getTime());
System.out.println(date1);
}
@Test
public void testCalendar(){
//1.实例化
//方式1:创建其子类(GregorianCalendar)的对象
//方式2:调用它的静态方法
Calendar calendar=Calendar.getInstance();
System.out.println(calendar);
//2.常用方法
//get()
int days = calendar.get(DAY_OF_MONTH);
System.out.println(days);
days = calendar.get(Calendar.DAY_OF_YEAR);
System.out.println(days);
//set()
//Calendar可变性
calendar.set(DAY_OF_MONTH,28);
days=calendar.get(DAY_OF_MONTH);
System.out.println(days);
//add()
calendar.add(DAY_OF_MONTH,-7);
days=calendar.get(DAY_OF_MONTH);
System.out.println(days);
//getTime() -- 日历类-->Date
Date date = calendar.getTime();
System.out.println(date);
//setTime() -- Date-->日历类
Date date1=new Date();
calendar.setTime(date1);
days=calendar.get(DAY_OF_MONTH);
System.out.println(days);
}
}
package com.xt.java2;
import org.junit.Test;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
public class JDK8DateTimeTest {
@Test
public void testDate(){
//偏移量
Date date=new Date(2022-1900,4-1,28);
System.out.println(date);//Thu Apr 28 00:00:00 CST 2022
}
@Test
public void test1(){
//获取当前的日期、时间、日期+时间
LocalTime localTime = LocalTime.now();
LocalDate localDate = LocalDate.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localTime);
System.out.println(localDate);
System.out.println(localDateTime);
//of():设置指定的年、月、日、时、分、秒是没有偏移量的
LocalDateTime localDateTime1 = localDateTime.of(2022, 4, 28, 19, 00, 43);
System.out.println(localDateTime1);
//getXxx()获取相关的属性
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getDayOfWeek());
System.out.println(localDateTime.getMonth());
System.out.println(localDateTime.getYear());
//体现以上3个类的不可变性
//withXxx():设置相关属性
LocalDate localDate1 = localDate.withDayOfMonth(22);
System.out.println(localDate);
System.out.println(localDate1);
LocalDateTime localDateTime2 = localDateTime.withHour(5);
System.out.println(localDateTime);
System.out.println(localDateTime2);
//不可变性
LocalDateTime plusMonths = localDateTime.plusMonths(3);
System.out.println(plusMonths);
LocalDateTime minusDays = localDateTime.minusDays(12);
System.out.println(minusDays);
}
@Test
public void test2(){
//now获取本初子午线对应的标准时间
Instant instant = Instant.now();
System.out.println(instant);
//添加时间的偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);
//toEpochMilli():获取自1970年1月1日0时0分0秒对应的毫秒数 -->Date类的getTime()
long epochMilli = instant.toEpochMilli();
System.out.println(epochMilli);
//ofEpochMilli():通过给定的毫秒数,获取Instant实例 -->Date(Long millis)
Instant ofEpochMilli = Instant.ofEpochMilli(1650593009205L);
System.out.println(ofEpochMilli);
}
@Test
public void test3(){
//方式一:预定义的标准格式
DateTimeFormatter formatter=DateTimeFormatter.ISO_LOCAL_DATE_TIME;
//格式化:日期-->字符串
LocalDateTime localDateTime=LocalDateTime.now();
String format = formatter.format(localDateTime);
System.out.println(localDateTime);
System.out.println(format);
//解析:字符串-->日期
TemporalAccessor parse = formatter.parse("2022-04-22T11:22:15.2600813");
System.out.println(parse);
//方式二:本地化相关的格式:ofLocalizedDateTime
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
String str = formatter1.format(localDateTime);
System.out.println(str); //2022/4/22 上午11:31
//会报异常
// DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
// String str2 = formatter2.format(localDateTime);
// System.out.println(str2); //2022/4/22 上午11:31
//(重点)方式三:自定义的格式,如:ofPattern("yyyy-MM-dd hh:mm:ss")
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
//格式化
String format1 = formatter2.format(LocalDateTime.now());
System.out.println(format1);
//解析
TemporalAccessor accessor = formatter2.parse("2022-04-22 04:13:28");
System.out.println(accessor);
}
}