Skip to content

Commit e6b9259

Browse files
java8日期处理
1 parent 8b097e9 commit e6b9259

File tree

2 files changed

+45
-30
lines changed

2 files changed

+45
-30
lines changed

src/main/java/com/date/DateDemo.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import java.util.Date;
88

99
public class DateDemo {
10-
public static void main(String args[]){
10+
11+
private static void formatCurrentTime() {
1112
Date date1=new Date();
1213
long currentTime = System.currentTimeMillis();
1314

@@ -17,19 +18,16 @@ public static void main(String args[]){
1718

1819
System.out.println(date1);
1920
System.out.println(formatter.format(date2));
20-
21-
22-
2321
}
2422

25-
public void dateToString(Date date) {
23+
public static void dateToString(Date date) {
2624
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
2725
String str=sdf.format(date);
2826
System.out.println(date+"转化为字符串是:"+str);
2927
}
3028

3129

32-
public void stringToDate() {
30+
public static void stringToDate() {
3331
//试着用SimpleDateFormat的parse方法将字符串转化为date类型
3432
//MM必须大写。。区分月份和时间
3533
DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@@ -44,7 +42,7 @@ public void stringToDate() {
4442
}
4543
}
4644

47-
public void stringToDate2() {
45+
public static void stringToDate2() {
4846
String str="2016-07-21 15:41:52";
4947
try {
5048
Date date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(str);
@@ -58,4 +56,10 @@ public void stringToDate2() {
5856
}
5957
}
6058

59+
public static void compareDate(Date date1,Date date2) {
60+
if (date1.compareTo(date2) == 0) {
61+
System.out.println(date1+"等于"+date2);
62+
}
63+
}
64+
6165
}

src/main/java/com/java8/localdate/LocalDateDemo.java

+34-23
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,57 @@
1313
public class LocalDateDemo {
1414

1515

16-
public static void main(String[] args) {
17-
18-
// timeToString();
19-
20-
// stringToTime();
21-
22-
// localDateTest();
23-
24-
monthDiff();
25-
}
26-
27-
private static void timeToString() {
16+
public void timeToString() {
2817
// 日期时间转字符串
2918
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
30-
//当前时间
31-
LocalDateTime now = LocalDateTime.now();
32-
String nowText = now.format(formatter);
33-
System.out.println("nowText为:" + nowText);
19+
LocalDateTime dateTime = LocalDateTime.of(2020, 1, 8, 15, 16, 17);
20+
String dateTimeStr = dateTime.format(formatter);
21+
System.out.println("dateTimeStr为:" + dateTime);
3422
}
3523

36-
private static void stringToTime() {
24+
public void stringToTime() {
3725
// 字符串转日期时间
3826
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
3927
String datetimeText = "2020-04-01 23:59:59";
40-
LocalDateTime localDateTime= LocalDateTime.parse(datetimeText, formatter);
28+
LocalDateTime localDateTime = LocalDateTime.parse(datetimeText, formatter);
4129
// LocalDateTime的toString()方法,在日期和时间中间加入了"T"字符串
4230
System.out.println(localDateTime);
4331
}
4432

45-
private static void localDateTest() {
46-
LocalDate date = LocalDate.of(2000, 1, 1);
47-
System.out.println("构造的日期为:" + date);
33+
public void generateLocalDate() {
34+
LocalDate date = LocalDate.of(2000, 1, 8);
35+
LocalDateTime dateTime = LocalDateTime.of(2020, 1, 8, 15, 16, 17);
36+
System.out.println("date:" + date);
37+
System.out.println("dateTime:" + dateTime);
38+
}
39+
40+
public void getLocalDateTimeMonth() {
41+
LocalDateTime dateTime = LocalDateTime.of(2020, 1, 8, 15, 16, 17);
42+
int year=dateTime.getYear();
43+
int month=dateTime.getMonth().getValue();
44+
int day=dateTime.getDayOfMonth();
45+
System.out.println("dateTime对应的年份为:"+year);
46+
System.out.println("dateTime对应的年份为:"+month);
47+
System.out.println("dateTime对应的年份为:"+day);
4848
}
4949

50-
private static void monthDiff(){
50+
public void monthDiff() {
5151
LocalDate date1 = LocalDate.of(2019, 1, 15);
5252
LocalDate date2 = LocalDate.of(2019, 3, 10);
5353
Period period = Period.between(date1, date2);
54-
System.out.println("相差的年数为"+period.getYears() + ",相差的月数为:" + period.getMonths() +
54+
System.out.println("相差的年数为" + period.getYears() + ",相差的月数为:" + period.getMonths() +
5555
",相差的天数为:" + period.getDays());
5656
}
5757

58+
public void addMonthDay() {
59+
LocalDateTime dt = LocalDateTime.of(2020, 1, 26, 20, 30, 59);
60+
System.out.println(dt);
61+
// 加5天减3小时:
62+
LocalDateTime dt2 = dt.plusDays(5).minusHours(3);
63+
System.out.println(dt2); // 2019-10-31T17:30:59
64+
// 减1月:
65+
LocalDateTime dt3 = dt2.minusMonths(1);
66+
System.out.println(dt3); // 2019-09-30T17:30:59
67+
}
68+
5869
}

0 commit comments

Comments
 (0)