package java1;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* SimpleDateFormat
* @author Baobao
* @create 2021-08-04 22:11
*/
public class DateTimeTest {
/*
SimpleDateFormat的使用:对日期date类的格式化和解析
1.两个操作:
格式化 日期-->字符串
解析 字符串-->日期
2.实例化
*/
@Test
public void testSimpleDateFormat() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat();
//格式化 日期-->字符串
Date date = new Date();
//System.out.println(date);
String format = sdf.format(date);
System.out.println(format);
//解析 字符串-->日期
String str="19-12-18 上午11:09";
Date date1 = sdf.parse(str);
System.out.println(date1);
//****************按照指定的方式格式化和解析*********************
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String str1 = format1.format(date1);
System.out.println(str1);
}
@Test
public void testExer() throws ParseException {
String birth="2020-09-08";
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
Date date = format1.parse(birth);
System.out.println(date);
java.sql.Date birthDate=new java.sql.Date(date.getTime());
System.out.println(birthDate);
}
/*
calender(抽象类)的使用
*/
@Test
public void testCalender(){
//实例化
//方式1:创建其子类的对象
//方式2:调用静态方法
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getClass());
//常用方法:
//get操作
int days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
//set操作
calendar.set(Calendar.DAY_OF_WEEK_IN_MONTH,22);
int days1 = calendar.get(Calendar.DAY_OF_MONTH);
//add操作
calendar.add(Calendar.DAY_OF_WEEK_IN_MONTH,3);
//getTime操作
Date calendarTime = calendar.getTime();
//setTime操作
calendar.setTime(calendarTime);
}
}
package java1;
import org.junit.Test;
import java.time.*;
import java.util.Date;
/**
* @author Baobao
* @create 2021-08-05 11:14
*/
public class JDK8CalenderTest {
@Test
public void testDate(){
Date date1=new Date(2020-1900,9,8);
System.out.println(date1);
}
@Test
public void test1(){
//now()获取当前的日期,时间or时间+日期
LocalDate localDate=LocalDate.now();
LocalTime now = LocalTime.now();
LocalDateTime localDateTime=LocalDateTime.now();
//of方法:设置指定的年月日,时分秒,没有偏移量
LocalDateTime time1 = LocalDateTime.of(2020, 10, 6, 13, 15);
System.out.println(time1);
//getxxx()
System.out.println(time1.getDayOfMonth());
System.out.println(time1.getDayOfWeek());
//不可变性
LocalDateTime localDateTime1 = time1.withDayOfMonth(22);
LocalDateTime localDateTime2 = localDateTime1.plusDays(3);
}
/*
instance的使用
*/
@Test
public void test02(){
Instant instant = Instant.now();
System.out.println(instant);
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);
long milli = instant.toEpochMilli();
System.out.println(milli);
}
/*
DateTimeFormatter:格式化解析日期or时间,类似于SimpleDateFormat
*/
@Test
public void test03(){
}
}