如何在java中将字符串日期转换为时间戳?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2742380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 12:20:31  来源:igfitidea点击:

How to convert string date to Timestamp in java?

java

提问by lakshmi

I want to convert string Date into Timestamp in java. The following coding i have written.I have declare the date for date1 is: 7-11-11 12:13:14.

我想在java中将字符串日期转换为时间戳。我编写的以下代码。我已声明 date1 的日期是:7-11-11 12:13:14。

SimpleDateFormat datetimeFormatter1 = new SimpleDateFormat(
                "yyyy-MM-dd hh:mm:ss");
Date lFromDate1 = datetimeFormatter1.parse(date1);
System.out.println("gpsdate :" + lFromDate1);
Timestamp fromTS1 = new Timestamp(lFromDate1.getTime());

I want to convert 7-11-11 12:13:14 this string date into timestamp. Now i got the output is 0007-11-11 00:13:14.000000 +05:30:00 but i want ( 7-11-11 12:13:14) this format Timestamp date. Can anyone please help me. Thank you.

我想将 7-11-11 12:13:14 这个字符串日期转换成时间戳。现在我得到的输出是 0007-11-11 00:13:14.000000 +05:30:00 但我想要(7-11-11 12:13:14)这种格式的时间戳日期。谁能帮帮我吗。谢谢你。

采纳答案by Glen Robertson

All you need to do is change the string within the java.text.SimpleDateFormatconstructor to: "MM-dd-yyyy HH:mm:ss".

您需要做的就是将java.text.SimpleDateFormat构造函数中的字符串更改为:“MM-dd-yyyy HH:mm:ss”。

Just use the appropriate letters to build the above string to match your input date.

只需使用适当的字母来构建上述字符串以匹配您的输入日期。

回答by MeBigFatGuy

Use capital HHto get hour of day format, instead of am/pm hours

使用大写HH来获取一天中的小时格式,而不是 am/pm 小时

回答by Sireesh Yarlagadda

You can even try this.

你甚至可以试试这个。

   String date="09/08/1980";    // take a string  date
    Timestamp ts=null;  //declare timestamp
    Date d=new Date(date); // Intialize date with the string date
    if(d!=null){  // simple null check
        ts=new java.sql.Timestamp(d.getTime()); // convert gettime from date and assign it to your timestamp.
    }

回答by Basil Bourque

tl;dr

tl;博士

java.sql.Timestamp                  
.valueOf(                           // Class-method parses SQL-style formatted date-time strings.
    "2007-11-11 12:13:14"
)                                   // Returns a `Timestamp` object.
.toInstant()                        // Converts from terrible legacy classes to modern *java.time* class.

java.sql.Timestamp.valueOfparses SQL format

java.sql.Timestamp.valueOf解析 SQL 格式

If you can use the full four digits for the year, your input string of 2007-11-11 12:13:14would be in standard SQL format assuming this value is meant to be in UTC time zone.

如果您可以使用完整的四位数作为年份,则您的输入字符串2007-11-11 12:13:14将采用标准 SQL 格式,假设该值是在 UTC 时区。

The java.sql.Timestampclass has a valueOfmethod to directly parse such strings.

java.sql.Timestamp类有一个valueOf直接解析这样的字符串的方法。

String input = "2007-11-11 12:13:14" ;
java.sql.Timestamp ts = java.sql.Timestamp.valueOf( input ) ;

java.time

时间

In Java 8 and later, the java.time framework makes it easier to verify the results. The j.s.Timestamp class has a nasty habit of implicitly applying your JVM's current default timestamp when generating a string representation via its toStringmethod. In contrast, the java.time classes by default use the standard ISO 8601formats.

在 Java 8 及更高版本中,java.time 框架可以更轻松地验证结果。jsTimestamp 类有一个坏习惯,即在通过其toString方法生成字符串表示时隐式应用 JVM 的当前默认时间戳。相比之下,java.time 类默认使用标准ISO 8601格式。

System.out.println( "Output: " + ts.toInstant().toString() );

回答by Jenya Miachyn

You can convert String to Timestamp:

您可以将字符串转换为时间戳:

String inDate = "01-01-1990"
DateFormat df = new SimpleDateFormat("MM-dd-yyyy");
Timestamp ts = new Timestamp(((java.util.Date)df.parse(inDate)).getTime());

回答by Navdeep Ghotra

Use below code to convert String Date to Epoc Timestamp. Note : - Your input Date format should match with SimpleDateFormat.

使用以下代码将字符串日期转换为 Epoc 时间戳。注意: - 您的输入日期格式应与 SimpleDateFormat 匹配。

String inputDateInString= "8/15/2017 12:00:00 AM";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyy hh:mm:ss");

Date parsedDate = dateFormat.parse("inputDateInString");

Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());

System.out.println("Timestamp "+ timestamp.getTime());