我正在将一些数据导入某个系统,它要求我为 CSV 文档中的日期提供一个 java 日期格式字符串。我不是 Java 程序员,我很难测试它。它要求使用对SimpleDateFormat有效的语法。
我有这样的日期Wednesday, January 2, 2013 5:29:26 PM +02:00
,这是我根据文档附带的格式字符串,但是,它失败了:
EEEE, MMM d, y hh:mm:ss a Z
我认为时区格式字符串是错误的,但我不知道正确的解决方案。请帮忙。
我正在将一些数据导入某个系统,它要求我为 CSV 文档中的日期提供一个 java 日期格式字符串。我不是 Java 程序员,我很难测试它。它要求使用对SimpleDateFormat有效的语法。
我有这样的日期Wednesday, January 2, 2013 5:29:26 PM +02:00
,这是我根据文档附带的格式字符串,但是,它失败了:
EEEE, MMM d, y hh:mm:ss a Z
我认为时区格式字符串是错误的,但我不知道正确的解决方案。请帮忙。
问题是 +02:00,Java 期待 +0200。如果您可以更正此问题,它将正确解析
Consider Joda Time library, which has built in support for parsing and outputting ISO-format date strings.
要调试日期解析错误,请打印出日期格式。
例如:
System.out.println(new SimpleDateFormat("EEEE, MMM d, y hh:mm:ss a Z").format(new Date()));
然后调整直到你看到正确的格式。
希望有帮助
希望您的问题的答案:
C:\Users\Cebence\jdev\StackOverflow> java StackOverflow15914753
input = Wednesday, January 2, 2013 5:29:26 PM +02:00
Parsing input into date ............ [ OK ]
date @ local = Wed Jan 02 16:29:26 CET 2013
ISO date @ local = 2013-01-02T16:29:26.000+01:00
UTC date = 2013-01-02T15:29:26.000Z
此输出由以下演示应用程序创建。请注意正确的时区、语言环境和“X”而不是“Z”进行解析的重要性。“X”处理“+02:00”,仅适用于 Java 7。
// Important to be US!
SimpleDateFormat format = new SimpleDateFormat(CUSTOM_FORMAT_WITH_TZ, Locale.US);
// Important to be +2 hours offset!
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/**
* http://stackoverflow.com/questions/15914753/java-date-format-string-for-date-wednesday-january-2-2013-52926-pm-0200
* input: "Wednesday, January 2, 2013 5:29:26 PM +02:00"
* asking-for: working timestamp format string "EEEE, MMM d, y hh:mm:ss a Z"
*
* @author dejan.cebetarevic
*/
public class StackOverflow15914753 {
private static final String ISO_FORMAT_WITH_TZ = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
private static final String ISO_FORMAT_NO_TZ = "yyyy-MM-dd'T'HH:mm:ss.SSS";
private static final String CUSTOM_FORMAT_WITH_TZ = "EEEE, MMMM d, y hh:mm:ss a XXX";
private static final String DEFAULT_INPUT = "Wednesday, January 2, 2013 5:29:26 PM +02:00";
public static void main(String[] args) throws Exception {
// Assume default input.
String input = DEFAULT_INPUT;
// But if an input was given, use it.
if (args.length == 1) {
input = args[0];
}
System.out.println("input = " + input);
System.out.print("\nParsing input into date ............");
Date d = createCustomDateFormat().parse(input);
System.out.println(" [ OK ]\n");
System.out.println(" date @ local = " + d);
System.out.println("ISO date @ local = " + isoFormat(d));
System.out.println(" UTC date = " + utcFormat(d));
}
public static String utcFormat(Date date) throws Exception {
SimpleDateFormat utc = new SimpleDateFormat(ISO_FORMAT_WITH_TZ, Locale.UK);
utc.setCalendar(Calendar.getInstance(TimeZone.getTimeZone("UTC")));
return utc.format(date);
}
public static String isoFormat(Date date) throws Exception {
SimpleDateFormat iso = new SimpleDateFormat(ISO_FORMAT_WITH_TZ);
return iso.format(date);
}
public static SimpleDateFormat createCustomDateFormat() throws Exception {
// Important to be US!
SimpleDateFormat format = new SimpleDateFormat(CUSTOM_FORMAT_WITH_TZ, Locale.US);
// Important to be +2 hours offset!
format.setCalendar(Calendar.getInstance(TimeZone.getTimeZone("GMT+2")));
return format;
}
}