java java将字符串格式的日期转换为时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14723872/
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-10-31 17:09:19  来源:igfitidea点击:

java converting date in string format to timestamp

java

提问by shakkir3435

I am trying to convert a date in this format: "Fri Mar 1, 2013 4:30 PM"to a timestamp value in this format: yyyy-mm-dd hh:mm:ss.

我正在尝试将这种格式的日期:“Fri Mar 1, 2013 4:30 PM”转换为这种格式的时间戳值: yyyy-mm-dd hh:mm:ss

example:

例子:

String str = 'Fri Mar 1, 2013 4:30 PM'should output: "2013-01-14 23:59:59"

String str = 'Fri Mar 1, 2013 4:30 PM'应该输出: "2013-01-14 23:59:59"

Here is what I've tried:

这是我尝试过的:

String string = "January 2, 2010";
Date date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(string); 
System.out.println(date);

This outputs: Sat Jan 02 00:00:00 GMT+05:30 2010

这输出: Sat Jan 02 00:00:00 GMT+05:30 2010

Thanks in Advance

提前致谢

回答by Hardik Mishra

Assuming you are getting date and time as Date. So, You need formatmethod of SimpleDateFormatter. Explore more patterns in the API.

假设您将日期和时间作为日期。所以,你需要SimpleDateFormatter 的format方法。探索 API 中的更多模式。

public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
        Date currDate = new Date();
        System.out.println("Current Date: " + currDate);
        System.out.println("Formatted Date: " + sdf.format(currDate));
}

Output:

输出:

Current Date: Wed Feb 06 13:15:19 IST 2013
Formatted Date: 2013-02-06 13:15:750

If you have date in string format, you need to parseit in Date first and then format it.

如果您有字符串格式的日期,则需要先将其解析为日期,然后再对其进行格式化。

Example:

例子:

   String str = "Fri Mar 1, 2013 4:30 PM";
   SimpleDateFormat sdf2 = new SimpleDateFormat("E MMM dd, yyyy HH:mm a"); 
   Date parsedDate = sdf2.parse(str);
   System.out.println("Parsed Date: " + parsedDate);

   sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS a");
   System.out.println("Formatted Date: " + sdf2.format(parsedDate));

Output:

输出:

Parsed Date: Fri Mar 01 04:30:00 IST 2013
Formatted Date: 2013-03-01 04:30:00 AM

回答by Ramadas

String str = "Fri Mar 1, 2013 4:30 PM";          
SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM dd, yyyy hh:mm a");
Date date = sdf1.parse(str);
System.out.println("Date Object:" + date);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm a");
System.out.println("Formatted Date:" + sdf2.format(date));

Output:

输出:

Date Object: Fri Mar 01 16:30:00 EST 2013 

Formatted Date: 2013-03-01 16:30 PM

回答by Ole V.V.

The two previous answers were good answers in 2013. Time is moving on, and so is the handling of time information in Java. If you can use Java 8, do yourself the favour of using the date and time classes in the new java.timepackage (also backported to Java 6 and 7 in the ThreeTen Backport):

前两个答案是 2013 年的好答案。时间在前进,Java 中时间信息的处理也是如此。如果您可以使用 Java 8,请java.time自行使用新包中的日期和时间类(也在 ThreeTen Backport 中向后移植到 Java 6 和 7):

    String str = "Fri Mar 1, 2013 4:30 PM";
    LocalDateTime ldt = LocalDateTime.parse(str, 
            DateTimeFormatter.ofPattern("EEE MMM d, uuuu h:mm a", Locale.ENGLISH));

This produces a time without time zone of 2013-03-01T16:30. To format it into the desired output format:

这将产生一个没有时区的时间2013-03-01T16:30。要将其格式化为所需的输出格式:

    System.out.println(ldt.format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss")));

This prints:

这打印:

2013-03-01 16:30:00

Should your want your timestamp in UTC, you may convert from your computer's time zone like this:

如果你想要你的 UTC 时间戳,你可以像这样从你的计算机的时区转换:

ldt.atZone(ZoneId.systemDefault()).toInstant()

Since I am in the Central European Time zone, on my computer it produces a point in time of 2013-03-01T15:30:00Z(where Zsignifies UTC; Instantobjects are always printed in UTC).

由于我在中欧时区,因此在我的计算机上它会生成一个时间点2013-03-01T15:30:00Z(其中Z表示 UTC;Instant对象始终以 UTC 打印)。

Link: ThreeTen Backport Home

链接:ThreeTen Backport 主页