在java中的时间戳字符串到时间戳
问题描述:
我有以下字符串“2015-04-02 11:52:00 + 02”,我需要在Java中将它解析为时间戳。 我试过各种格式,包括在java中的时间戳字符串到时间戳
SimpleDateFormat mdyFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss+Z");
,但似乎没有任何工作 - 我不断收到一个ParseException
谁能帮助?
我需要这样的东西:
SimpleDateFormat mdyFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss+Z");
Timestamp t = new Timestamp(mdyFormat.parse("2015-04-02 11:52:00+02").getTime());
答
尝试这个
String str="2009-12-31 23:59:59 +0100";
/\
||
Provide Space while providing timeZone
SimpleDateFormat mdyFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
System.out.println(mdyFormat.parse(str));
输出
Fri Jan 01 04:29:59 IST 2010
答
的java.sql.Timestamp对象不具有时区 - 他们在时刻,像java.util.Date
那么试试这个:
SimpleDateFormat mdyFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp t = new Timestamp(mdyFormat.parse("2015-04-02 11:52:00").getTime());
答
ISO 8601
替换空间与T
中间,你有一个有效的标准(ISO 8601)的字符串,可以直接通过任一Joda-Time库或内置到Java 8.0中的新java.time package解析格式(灵感乔达时间)。在数百个示例中搜索*。
如果使用java.time,请阅读我对有关解析小时偏移值时的错误的问题的评论。
Joda-Time中的示例2.7。
String inputRaw = "2015-04-02 11:52:00+02";
String input = inputRaw.replace(" ", "T");
DateTimeZone zone = DateTimeZone.forID("America/Montreal"); // Specify desired time zone adjustment.
DateTime dateTime = new DateTime(input, zone);
张贴您的StackTrace! – 2015-04-02 14:18:05
线程“main”中的异常java.text.ParseException:Unparseable date:“2015-04-02 11:52:00 + 02” \t at java.text.DateFormat.parse(Unknown Source) \t at com .. ..... Main.main(Main.java:45) – Alexandr 2015-04-02 14:20:12
不知道你是否可以使用Java 8,但[这篇文章](http://*.com/questions/22463062/how-to-parse-format -dates-with-localdatetime-java-8)可以帮助你。 – Pete 2015-04-02 14:38:49