Skip to content

Commit 8b097e9

Browse files
整理代码块
1 parent 8f41ea5 commit 8b097e9

21 files changed

+344
-135
lines changed

.classpath

Lines changed: 0 additions & 6 deletions
This file was deleted.

.project

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,47 @@
11
package com.date;
22

3+
import java.text.ParseException;
4+
import java.text.SimpleDateFormat;
35
import java.util.Calendar;
4-
import java.util.GregorianCalendar;
6+
import java.util.Date;
57

8+
/**
9+
* @Author: EnjoyCoding
10+
* @Date: 2020\5\1 0001 16:17
11+
* @Description:
12+
*/
613
public class CalendarDemo {
7-
public static void main(String args[]){
8-
Calendar calenda=new GregorianCalendar(); //GregorianCalendar是日历的子类。
9-
System.out.println("年:"+calenda.get(Calendar.YEAR)); //Calendar.YEAR是常量。意指年
1014

15+
public static void setCalendarTime() throws ParseException {
16+
String dateStr="2018-07-07 07:08:11";
17+
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
18+
Date date = simpleDateFormat.parse(dateStr);
19+
Calendar calendar = Calendar.getInstance();
20+
//通过calendar设置时间
21+
calendar.setTime(date);
22+
//获取calendar对应的时间
23+
Date time = calendar.getTime();
24+
System.out.println(time);
25+
}
1126

12-
}
27+
public static void setCalendarMonthDay() {
28+
Calendar calendar = Calendar.getInstance();
29+
//通过Calendar设置年月日
30+
calendar.set(Calendar.MONTH, 7);
31+
calendar.set(Calendar.DAY_OF_MONTH, 7);
32+
calendar.set(Calendar.YEAR, 2018);
33+
calendar.set(Calendar.HOUR_OF_DAY,19);
34+
//通过Calendar获取时间,年月日
35+
Date date = calendar.getTime();
36+
int year = calendar.get(Calendar.YEAR);
37+
//Calendar的月份是从0开始计算的,所以要加一才是真实的月份
38+
int month = calendar.get(Calendar.MONTH) + 1;
39+
int day = calendar.get(Calendar.DAY_OF_MONTH);
40+
int hour = calendar.get(Calendar.HOUR_OF_DAY);
41+
System.out.println(date);
42+
System.out.println("year:" + year);
43+
System.out.println("month:" + month);
44+
System.out.println("day:" + day);
45+
System.out.println("hour:" + hour);
46+
}
1347
}

src/main/java/com/date/CalendarDemo2.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.date;
22

3+
import javax.xml.crypto.Data;
4+
import java.text.DateFormat;
5+
import java.text.ParseException;
36
import java.text.SimpleDateFormat;
47
import java.util.Date;
58

@@ -18,4 +21,41 @@ public static void main(String args[]){
1821

1922

2023
}
24+
25+
public void dateToString(Date date) {
26+
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
27+
String str=sdf.format(date);
28+
System.out.println(date+"转化为字符串是:"+str);
29+
}
30+
31+
32+
public void stringToDate() {
33+
//试着用SimpleDateFormat的parse方法将字符串转化为date类型
34+
//MM必须大写。。区分月份和时间
35+
DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
36+
String str="2016-5-11 11:06:59";
37+
Date date=null;
38+
try{
39+
/*Date类型的parse方法已经过时了,不可用.一般用SimpleDateFormat的parse */
40+
date=dateFormat.parse(str);
41+
System.out.println("今天的日期是:"+date);
42+
}catch(ParseException e) {
43+
e.printStackTrace();
44+
}
45+
}
46+
47+
public void stringToDate2() {
48+
String str="2016-07-21 15:41:52";
49+
try {
50+
Date date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(str);
51+
System.out.println(DateFormat.getDateInstance().format(date));
52+
System.out.println(new SimpleDateFormat("yyyy年MM月dd日").format(date));
53+
System.out.println(new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(date));
54+
System.out.println(new SimpleDateFormat("yyyy/MM/dd HH/mm/ss").format(date));
55+
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
56+
}catch(Exception e) {
57+
e.printStackTrace();
58+
}
59+
}
60+
2161
}

src/main/java/com/date/DateTime.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/main/java/com/date/DateToString.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/main/java/com/date/SimpleDateFormatDemo.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/main/java/com/date/StringToDate.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/main/java/com/java8/lambda/LambdaRunnable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
public class LambdaRunnable {
44
public static void main(String[] args) {
5-
Runnable race2 = () -> System.out.println("Hello world !");
5+
Runnable race2 = () -> System.out.println("HelloWorld world !");
66
race2.run();
77

88
// 传统的代码表现方式如下:
99
// Runnable race1 = new Runnable() {
1010
// @Override
1111
// public void run() {
12-
// System.out.println("Hello world !");
12+
// System.out.println("HelloWorld world !");
1313
// }
1414
// };
1515
// race1.run
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.java8.localdate;
2+
3+
import java.time.LocalDate;
4+
import java.time.LocalDateTime;
5+
import java.time.Period;
6+
import java.time.format.DateTimeFormatter;
7+
8+
/**
9+
* @Author: EnjoyCoding
10+
* @Date: 2020\4\15 0015 23:31
11+
* @Description:
12+
*/
13+
public class LocalDateDemo {
14+
15+
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() {
28+
// 日期时间转字符串
29+
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);
34+
}
35+
36+
private static void stringToTime() {
37+
// 字符串转日期时间
38+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
39+
String datetimeText = "2020-04-01 23:59:59";
40+
LocalDateTime localDateTime= LocalDateTime.parse(datetimeText, formatter);
41+
// LocalDateTime的toString()方法,在日期和时间中间加入了"T"字符串
42+
System.out.println(localDateTime);
43+
}
44+
45+
private static void localDateTest() {
46+
LocalDate date = LocalDate.of(2000, 1, 1);
47+
System.out.println("构造的日期为:" + date);
48+
}
49+
50+
private static void monthDiff(){
51+
LocalDate date1 = LocalDate.of(2019, 1, 15);
52+
LocalDate date2 = LocalDate.of(2019, 3, 10);
53+
Period period = Period.between(date1, date2);
54+
System.out.println("相差的年数为"+period.getYears() + ",相差的月数为:" + period.getMonths() +
55+
",相差的天数为:" + period.getDays());
56+
}
57+
58+
}

src/main/java/com/java8/optional/OrElseGetDemo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ public static void main(final String... args) {
3535

3636
public static String functionB() {
3737
System.out.println("functionB()...");
38-
return "B";
38+
return "Son";
3939
}
4040

4141
public static String functionA() {
4242
System.out.println("functionA()");
43-
// return "A";
43+
// return "Father";
4444
return null;
4545
}
4646

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.jvm;
2+
3+
/**
4+
* @Author: EnjoyCoding
5+
* @Date: 2020\4\9 0009 23:10
6+
* @Description:
7+
*/
8+
public class StringMemoryDemo {
9+
public static void main(String[] args) {
10+
strMemoryCompare();
11+
}
12+
13+
14+
/**
15+
* String内存模型
16+
*
17+
*/
18+
public static void strMemoryCompare() {
19+
//s1是变量,存放在栈里。"HelloWorld"存放在常量池中
20+
String s1 = "HelloWorld";
21+
//new String("HelloWorld")是个对象,存放在堆。
22+
String s2 = new String("HelloWorld");
23+
String s3 = "HelloWorld";
24+
String s4 = "World";
25+
// "HelloWorld" + "World" 在编译时会被优化成"HelloWorld"
26+
String s5 = "HelloWorld" + "World";
27+
// 通过加号拼接两个字符串,会在堆上创建的新的对象
28+
String s6 = s3 + s4;
29+
System.out.println(s1 == s2); //false,s1指向常量池中的"HelloWorld",s2指向对象String("HelloWorld")
30+
System.out.println(s1 == s5); //true,都指向常量池的"HelloWorld"
31+
System.out.println(s1 == s6); //false,因为s3+s4相当于创建了新对象.
32+
// intern()方法会监测常量池中是否有该字符串的值的字面量,有,返回字符串常量的地址,没有,则新建新建字符串常量并返回内存地址。
33+
System.out.println(s1 == s6.intern()); //true,都指向常量池中的"HelloWorld"
34+
System.out.println(s2 == s2.intern()); //false,s2指向对象,s2.intern()指向常量池"HelloWorld"
35+
36+
37+
}
38+
39+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.jvm.staticdemo;
2+
3+
/**
4+
* @Author: EnjoyCoding
5+
* @Date: 2020\4\11 0011 20:30
6+
* @Description:
7+
*/
8+
public class Father {
9+
static {
10+
System.out.println("Father static");
11+
}
12+
13+
public Father() {
14+
System.out.println("Father Construct");
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.jvm.staticdemo;
2+
3+
/**
4+
* @Author: EnjoyCoding
5+
* @Date: 2020\4\11 0011 20:42
6+
* @Description:
7+
*/
8+
public class HelloWorld {
9+
public static void main(String[] args) {
10+
new Father();
11+
new Son();
12+
}
13+
}

0 commit comments

Comments
 (0)