<?xml version="1.0" encoding="UTF-8"?> <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.coveros.demo</groupId> <artifactId>helloworld</artifactId> <version>1.1</version> <packaging>jar</packaging> <name>Hello World</name> <description>The most basic of Java programs.</description> <url>https://www.coveros.com/</url> <inceptionYear>2018</inceptionYear> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <jdk.version>1.8</jdk.version> <maven.compiler.plugin.version>3.8.1</maven.compiler.plugin.version> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.compiler.plugin.version}</version> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> </plugins> </build> </project>
HelloWorld.java
package com.coveros.demo.helloworld; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class HelloWorld { public static void main(final String[] args) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //format.setTimeZone(java.util.TimeZone.getTimeZone("GMT-8")); Date date = null; try { date = format.parse("2020-11-10 12:34:56"); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } long unixSeconds = date.getTime(); System.out.println("timestamp="+unixSeconds); // convert seconds to milliseconds date = new java.util.Date(unixSeconds); format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //format.setTimeZone(java.util.TimeZone.getTimeZone("GMT-8")); String formattedDate = format.format(date); System.out.println(formattedDate); } }
build: $ mvn clean package
run: $ java -cp target/helloworld-1.0.jar com.coveros.demo.helloworld.HelloWorld
Folder Tree
$ tree . ├── pom.xml ├── src │ └── main │ └── java │ └── com │ └── coveros │ └── demo │ └── helloworld │ └── HelloWorld.java
Unix Time on line tool
沒有留言:
張貼留言