Skip to content

Commit

Permalink
时间戳测试
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim authored and Jim committed Nov 25, 2017
1 parent c40caf8 commit 79de0de
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions javabasicstest/test29/test1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
42 changes: 42 additions & 0 deletions javabasicstest/test29/test1/pom.xml
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>
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();
}
}
}
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());
}
}
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());
}
}
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 );
}
}
16 changes: 16 additions & 0 deletions javabasicstest/test29/test1/test1.iml
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>

0 comments on commit 79de0de

Please sign in to comment.