且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用java.text.SimpleDateFormat解析日期字符串

更新时间:2023-11-05 09:06:22

您是否可以使用正则表达式进行预处理,并替换时区,例如

  String dateAndTime = 。
String preprocessed = dateAndTime.replace(([+ - ])(\\\\d):( \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ $ 3);
//继续生活


I have a weird problem, I need to parse a date string that looks like 1997-02-14T00:00:00.0000000+05:30. The odd thing about the date string is the time zone information. It's +05:30 instead of the usual +0530.

I have the basic format string ready, yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZ which would have worked like a charm, if not for the TZ information.

Can anyone suggest a solution to this problem? Is there some kind format string which can handle that kind of TZ info?

Thanks in advance.

Can you not preprocess with a regex and replace the timezone e.g.

String dateAndTime = ...
String preprocessed = dateAndTime.replace("([+-])(\\d\\d):(\\d\\d)$", "$1$2$3");
// Go on with your life