常用类:Date
一、Java8版本之前日期时间的API
package com.hsy.commonClass.date;
import org.junit.Test;
import java.util.Date;
public class DateTimeTest {
@Test
public void test2(){
Date date1 = new Date();
System.out.println(date1.toString());
System.out.println(date1.getTime());
Date date2 = new Date(155030620410L);
System.out.println(date2.toString());
java.sql.Date date3 = new java.sql.Date(35235325345L);
System.out.println(date3);
Date date6 = new Date();
java.sql.Date date7 = new java.sql.Date(date6.getTime());
}
@Test
public void test1(){
long time = System.currentTimeMillis();
System.out.println(time);
}
}
二、SimpleDateFormat
@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:43";
Date date1 = sdf.parse(str);
System.out.println(date1);
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format1 = sdf1.format(date);
System.out.println(format1);
Date date2 = sdf1.parse("2020-02-18 11:48:27");
System.out.println(date2);
}
@Test
public void testExer() throws ParseException {
String birth = "2020-09-08";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf1.parse(birth);
java.sql.Date birthDate = new java.sql.Date(date.getTime());
System.out.println(birthDate);
}
三、Calendar
@Test
public void testCalendar(){
Calendar calendar = Calendar.getInstance();
int days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
calendar.set(Calendar.DAY_OF_MONTH,22);
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
calendar.add(Calendar.DAY_OF_MONTH,-3);
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
Date date = calendar.getTime();
System.out.println(date);
Date date1 = new Date();
calendar.setTime(date1);
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
}
四、jdk 8中日期时间API
package com.hsy.commonClass.date;
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 date1 = new Date(2020 - 1900,9 - 1,8);
System.out.println(date1);
}
@Test
public void test1(){
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);
LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10, 6, 13, 23, 43);
System.out.println(localDateTime1);
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getDayOfWeek());
System.out.println(localDateTime.getMonth());
System.out.println(localDateTime.getMonthValue());
System.out.println(localDateTime.getMinute());
LocalDate localDate1 = localDate.withDayOfMonth(22);
System.out.println(localDate);
System.out.println(localDate1);
LocalDateTime localDateTime2 = localDateTime.withHour(4);
System.out.println(localDateTime);
System.out.println(localDateTime2);
LocalDateTime localDateTime3 = localDateTime.plusMonths(3);
System.out.println(localDateTime);
System.out.println(localDateTime3);
LocalDateTime localDateTime4 = localDateTime.minusDays(6);
System.out.println(localDateTime);
System.out.println(localDateTime4);
}
@Test
public void test2(){
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);
Instant instant1 = Instant.ofEpochMilli(1550475314878L);
System.out.println(instant1);
}
@Test
public void test3(){
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDateTime localDateTime = LocalDateTime.now();
String str1 = formatter.format(localDateTime);
System.out.println(localDateTime);
System.out.println(str1);
TemporalAccessor parse = formatter.parse("2019-02-18T15:42:18.797");
System.out.println(parse);
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
String str2 = formatter1.format(localDateTime);
System.out.println(str2);
DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
String str3 = formatter2.format(LocalDate.now());
System.out.println(str3);
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
String str4 = formatter3.format(LocalDateTime.now());
System.out.println(str4);
TemporalAccessor accessor = formatter3.parse("2019-02-18 03:52:09");
System.out.println(accessor);
}
}