forked from easonjim/5_java_example
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jim
authored and
Jim
committed
Nov 25, 2017
1 parent
c40caf8
commit 79de0de
Showing
8 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.jsoft.testjavabasics</groupId> | ||
<artifactId>test1</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<packaging>jar</packaging> | ||
|
||
<name>test1</name> | ||
<url>http://maven.apache.org</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>3.8.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>joda-time</groupId> | ||
<artifactId>joda-time</artifactId> | ||
<version>2.6</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
42 changes: 42 additions & 0 deletions
42
javabasicstest/test29/test1/src/main/java/com/jsoft/testjavabasics/test1/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
javabasicstest/test29/test1/src/main/java/com/jsoft/testjavabasics/test1/TimeApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
javabasicstest/test29/test1/src/main/java/com/jsoft/testjavabasics/test1/TimeApp2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
javabasicstest/test29/test1/src/test/java/com/jsoft/testjavabasics/test1/AppTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8"> | ||
<output url="file://$MODULE_DIR$/target/classes" /> | ||
<output-test url="file://$MODULE_DIR$/target/test-classes" /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> | ||
<excludeFolder url="file://$MODULE_DIR$/target" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:3.8.1" level="project" /> | ||
<orderEntry type="library" name="Maven: joda-time:joda-time:2.6" level="project" /> | ||
</component> | ||
</module> |