JDK1.7日期时间操作
示例小结
public class DateTest {
public static final String FORMAT_DATE = "yyyy-MM-dd";
public static final String FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String FORMAT_MIN = "yyyy-MM-dd HH:mm";
public static final String FORMAT_MILLISECOND = "yyyy-MM-dd HH:mm:ss.S";
private static String[] parsePatterns = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm"};
/**
* SimpleDateFormat时间格式化
*/
@Test
public void test1() throws ParseException {
Date date = new Date();
/**
*常用格式:
* yyyy-MM-dd
* yyyy-MM-dd HH:mm:ss
* yyyy-MM-dd HH:mm
*/
//根据约定好的pattern时间格式进行格式化和解析
SimpleDateFormat dateFormat = new SimpleDateFormat(FORMAT);
//格式化
String dateStr = dateFormat.format(date);
System.out.println(dateStr);
//字符串解析成date对象
Date parseDate = dateFormat.parse(dateStr);
System.out.println(parseDate);
}
/**
* 2021-12-04 00:49:39
* Sat Dec 04 00:49:39 CST 2021
*/
/**
* Calendar(抽象类)日历类的使用
*/
@Test
public void test2() {
//两种创建
// Calendar calendar = new GregorianCalendar();
//默认用getInstance()创建
Calendar calendar = Calendar.getInstance();
//get()
//根据日历类型获取数据
//当天日历对应的一个月的第几天
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(day);
//set() 设置日期
calendar.set(Calendar.DAY_OF_MONTH, 9);
day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(day);
//添加天数,负数表示减天数
calendar.add(Calendar.DAY_OF_MONTH, -1);
day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(day);
//getTime() 日历类转Date
Date date = calendar.getTime();
System.out.println(date);
//根据Date日期设置日历
calendar.setTime(new Date());
day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(day);
}
/**
* 4
* 9
* 8
* Wed Dec 08 01:55:33 CST 2021
* 4
*/
}
JDK1.8日期时间操作——重点
LocalDateTime
示例小结
public class JDKDateTest {
@Test
public void test1() {
//创建日期对象
//获取当前日期时间 now()
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);
// 2021-12-04
// 23:44:44.112
// 2021-12-04T23:44:44.112
//创建指定日期时间的of()方法
LocalDateTime dateTime = LocalDateTime.of(2020, 10, 10, 12, 12, 12);
LocalDate date = LocalDate.of(2020, 10, 10);
LocalTime time = LocalTime.of(12, 12, 12);
System.out.println(dateTime);
System.out.println(date);
System.out.println(time);
// 2020-10-10T12:12:12
// 2020-10-10
// 12:12:12
//getXXX方法 获取时间里的年月日的值相关的属性
int dayOfYear = dateTime.getDayOfYear();
Month month = dateTime.getMonth();
int monthValue = dateTime.getMonthValue();
int dayOfMonth = dateTime.getDayOfMonth();
System.out.println(dayOfYear);
System.out.println(month);
System.out.println(monthValue);
System.out.println(dayOfMonth);
// 284
// OCTOBER
// 10
// 10
//withXXX() 设置指定的时间相关的属性返回新的日期,原来的日期是不变的
//LocalDateTime JDK时间不可变性,因此是线程安全的。
LocalDateTime withDateTime1 = dateTime.with(localDateTime);
LocalDateTime withDateTime2 = dateTime.withDayOfMonth(11);
System.out.println("原来日期:"+dateTime);
System.out.println("修改后日期1:"+withDateTime1);
System.out.println("修改后日期2:"+withDateTime2);
System.out.println("-----------------------------------");
// 原来日期:2020-10-10T12:12:12
// 修改后日期1:2021-12-04T23:44:44.112
// 修改后日期2:2020-10-11T12:12:12
//日期时间的添加删除
//可以直接添加想要添加的天数或其他属性值,返回的日期时间会自动处理为对应的日期时间
LocalDateTime plusDateTime = dateTime.plusDays(60);
LocalDateTime plusDateTime2 = dateTime.plusHours(70);
//加减时间都可以操作负数,负数对应就是相反的操作,没有问题
LocalDateTime minusDateTime1 = dateTime.minusDays(-1);
System.out.println("原来日期:"+dateTime);
System.out.println(plusDateTime);
System.out.println(plusDateTime2);
System.out.println(minusDateTime1);
// 原来日期:2020-10-10T12:12:12
// 2020-12-09T12:12:12
// 2020-10-13T10:12:12
// 2020-10-11T12:12:12
}
}
Instant——瞬时——类似于Date
示例小结
public class InstantTest {
@Test
public void test1() {
//获取本初子午线对应的当前时间
Instant instant = Instant.now();
long timeMillis = System.currentTimeMillis();
System.out.println(instant);
// 2021-12-04T16:16:27.593Z
//添加东八区时间的偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);
// 2021-12-05T00:16:27.593+08:00
//获取19701-1-1 00:00:00到现在的毫秒数,类似于System.currentTimeMillis()
long epochMilli = instant.toEpochMilli();
System.out.println("toEpochMilli "+epochMilli);
System.out.println("currentTimeMillis "+timeMillis);
// toEpochMilli 1638634587593
// currentTimeMillis 1638634587593
//根据时间戳获取Instant示例,类似于Date(long date)
Instant instant1 = Instant.ofEpochMilli(epochMilli);
System.out.println(instant1);
// 2021-12-04T16:16:27.593Z
}
}
DateTimeFormatter——格式化解析
时间格式化注意
yyyy-MM-dd HH:mm:ss HH表示24小时时间制
yyyy-MM-dd hh:mm:ss HH表示12小时时间制
代码示例
@Test
public void test2(){
LocalDateTime now = LocalDateTime.now();
//自定义格式化类型ofPattern()
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//格式化
String nowStr = formatter.format(now);
System.out.println(nowStr);
//2021-12-05 00:49:37
//解析
LocalDateTime dateTime = LocalDateTime.parse(nowStr, formatter);
System.out.println(dateTime);
//2021-12-05T00:49:37
TemporalAccessor parse1 = formatter.parse(nowStr);
System.out.println(parse1);
// {},ISO resolved to 2021-12-05T00:49:37
//强转报错,要使用LocalDateTime.parse(nowStr, formatter);
//java.lang.ClassCastException: java.time.format.Parsed cannot be cast to java.time.LocalDateTime
// LocalDateTime parse2 = (LocalDateTime) formatter.parse(nowStr);
// System.out.println(parse2);
}
其他
UTC 和 GMT的区别是什么啊?
GMT(Greenwish Mean Time 格林威治平时),这是UTC的民间名称。GMT=UTC
没有区别。
参考
480-488