From 79de0deef4e625ec1b6d37a12e2da79a3c597df7 Mon Sep 17 00:00:00 2001 From: Jim Date: Sat, 25 Nov 2017 20:36:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=88=B3=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 9 ++++ javabasicstest/test29/test1/.gitignore | 1 + javabasicstest/test29/test1/pom.xml | 42 +++++++++++++++ .../com/jsoft/testjavabasics/test1/App.java | 42 +++++++++++++++ .../jsoft/testjavabasics/test1/TimeApp.java | 51 +++++++++++++++++++ .../jsoft/testjavabasics/test1/TimeApp2.java | 22 ++++++++ .../jsoft/testjavabasics/test1/AppTest.java | 38 ++++++++++++++ javabasicstest/test29/test1/test1.iml | 16 ++++++ 8 files changed, 221 insertions(+) create mode 100644 javabasicstest/test29/test1/.gitignore create mode 100644 javabasicstest/test29/test1/pom.xml create mode 100644 javabasicstest/test29/test1/src/main/java/com/jsoft/testjavabasics/test1/App.java create mode 100644 javabasicstest/test29/test1/src/main/java/com/jsoft/testjavabasics/test1/TimeApp.java create mode 100644 javabasicstest/test29/test1/src/main/java/com/jsoft/testjavabasics/test1/TimeApp2.java create mode 100644 javabasicstest/test29/test1/src/test/java/com/jsoft/testjavabasics/test1/AppTest.java create mode 100644 javabasicstest/test29/test1/test1.iml diff --git a/.gitignore b/.gitignore index b83b6d9..6019877 100644 --- a/.gitignore +++ b/.gitignore @@ -149,3 +149,12 @@ springboottest/springboottest9/.idea/libraries/Maven__org_springframework_spring springboottest/springboottest9/.idea/libraries/Maven__org_springframework_spring_web_4_3_9_RELEASE.xml springboottest/springboottest9/.idea/libraries/Maven__org_springframework_spring_webmvc_4_3_9_RELEASE.xml springboottest/springboottest9/.idea/libraries/Maven__org_yaml_snakeyaml_1_17.xml +javabasicstest/test29/.idea/.name +javabasicstest/test29/.idea/compiler.xml +javabasicstest/test29/.idea/encodings.xml +javabasicstest/test29/.idea/kotlinc.xml +javabasicstest/test29/.idea/misc.xml +javabasicstest/test29/.idea/modules.xml +javabasicstest/test29/.idea/workspace.xml +javabasicstest/test29/.idea/libraries/Maven__joda_time_joda_time_2_6.xml +javabasicstest/test29/.idea/libraries/Maven__junit_junit_3_8_1.xml diff --git a/javabasicstest/test29/test1/.gitignore b/javabasicstest/test29/test1/.gitignore new file mode 100644 index 0000000..b83d222 --- /dev/null +++ b/javabasicstest/test29/test1/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/javabasicstest/test29/test1/pom.xml b/javabasicstest/test29/test1/pom.xml new file mode 100644 index 0000000..37ebdb6 --- /dev/null +++ b/javabasicstest/test29/test1/pom.xml @@ -0,0 +1,42 @@ + + 4.0.0 + + com.jsoft.testjavabasics + test1 + 0.0.1-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + jar + + test1 + http://maven.apache.org + + + UTF-8 + + + + + junit + junit + 3.8.1 + test + + + joda-time + joda-time + 2.6 + + + diff --git a/javabasicstest/test29/test1/src/main/java/com/jsoft/testjavabasics/test1/App.java b/javabasicstest/test29/test1/src/main/java/com/jsoft/testjavabasics/test1/App.java new file mode 100644 index 0000000..132509e --- /dev/null +++ b/javabasicstest/test29/test1/src/main/java/com/jsoft/testjavabasics/test1/App.java @@ -0,0 +1,42 @@ +package com.jsoft.testjavabasics.test1; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +/** + * Hello world! + * @author jim + * @date 2017/11/25 + */ +public class App { + public static void main(String[] args) { + // 精确到毫秒 + // 获取当前时间戳 + System.out.println(System.currentTimeMillis()); + System.out.println(Calendar.getInstance().getTimeInMillis()); + System.out.println(new Date().getTime()); + + // 精确到秒 + // 获取当前时间戳 + System.out.println(System.currentTimeMillis() / 1000); + System.out.println(Calendar.getInstance().getTimeInMillis() / 1000); + System.out.println(new Date().getTime() / 1000); + + // 精确到毫秒 + // 获取指定格式的时间 + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); + // 输出字符串 + System.out.println(df.format(new Date())); + // 获取指定时间Date对象,参数是时间戳,只能精确到秒 + System.out.println(new Date(1510369871)); + df.getCalendar(); + // 获取指定时间的时间戳 + try { + System.out.println(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS").parse("2017/11/11 11:11:11:111").getTime()); + } catch (ParseException e) { + e.printStackTrace(); + } + } +} diff --git a/javabasicstest/test29/test1/src/main/java/com/jsoft/testjavabasics/test1/TimeApp.java b/javabasicstest/test29/test1/src/main/java/com/jsoft/testjavabasics/test1/TimeApp.java new file mode 100644 index 0000000..2aac3f2 --- /dev/null +++ b/javabasicstest/test29/test1/src/main/java/com/jsoft/testjavabasics/test1/TimeApp.java @@ -0,0 +1,51 @@ +package com.jsoft.testjavabasics.test1; + +import org.joda.time.LocalDate; +import org.joda.time.format.DateTimeFormat; +import org.joda.time.format.DateTimeFormatter; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + + +/** + * This is Description + * + * @author jim + * @date 2017/11/25 + */ +public class TimeApp { + public static void main(String[] args) { + // Java 7及之前版本 + // 使用java.util.Calendar(不推荐) + Date date = null; + try { + date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2015-09-17 20:27:00"); + } catch (ParseException e) { + e.printStackTrace(); + } + Calendar now = Calendar.getInstance(); + now.setTime(date); + + int year = now.get(Calendar.YEAR); + int month = now.get(Calendar.MONTH) + 1; // 0-based! + int day = now.get(Calendar.DAY_OF_MONTH); + + System.out.println("year: " + year); + System.out.println("month: " + month); + System.out.println("day: " + day); + + // 时间已经改变,输出时间戳,可以看出这个是全局的,所以不建议使用全局的,直接使用时重新创建一个new SimpleDateFormat().getCalendar() + System.out.println(now.getTimeInMillis()); + + // joda-time(推荐),第三方库 + DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss"); + LocalDate localDate = formatter.parseLocalDate("2015-09-17 20:27:00"); + + System.out.println("yearOfCentury: " + localDate.getYearOfCentury()); + System.out.println("monthOfYear: " + localDate.getMonthOfYear()); + System.out.println("dayOfMonth: " + localDate.getDayOfMonth()); + } +} diff --git a/javabasicstest/test29/test1/src/main/java/com/jsoft/testjavabasics/test1/TimeApp2.java b/javabasicstest/test29/test1/src/main/java/com/jsoft/testjavabasics/test1/TimeApp2.java new file mode 100644 index 0000000..de4fd5b --- /dev/null +++ b/javabasicstest/test29/test1/src/main/java/com/jsoft/testjavabasics/test1/TimeApp2.java @@ -0,0 +1,22 @@ +package com.jsoft.testjavabasics.test1; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +/** + * This is Description + * + * @author jim + * @date 2017/11/25 + */ +public class TimeApp2 { + public static void main(String[] args) { + // Java 8,直接使用new datetime api (推荐!!) + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + LocalDateTime ldt = LocalDateTime.parse("2015-09-17 20:27:00", formatter); + + System.out.println("Year: " + ldt.getYear()); + System.out.println("Month: " + ldt.getMonth().getValue()); + System.out.println("DayOfMonth: " + ldt.getDayOfMonth()); + } +} diff --git a/javabasicstest/test29/test1/src/test/java/com/jsoft/testjavabasics/test1/AppTest.java b/javabasicstest/test29/test1/src/test/java/com/jsoft/testjavabasics/test1/AppTest.java new file mode 100644 index 0000000..f06535b --- /dev/null +++ b/javabasicstest/test29/test1/src/test/java/com/jsoft/testjavabasics/test1/AppTest.java @@ -0,0 +1,38 @@ +package com.jsoft.testjavabasics.test1; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} diff --git a/javabasicstest/test29/test1/test1.iml b/javabasicstest/test29/test1/test1.iml new file mode 100644 index 0000000..ac9cff0 --- /dev/null +++ b/javabasicstest/test29/test1/test1.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file