希望,回答您的問題:
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;
}
}
的問題是'+02:00',Java的期待'+ 0200'。如果你可以糾正這個問題,它會解析正確 – MadProgrammer 2013-04-10 00:30:39
你也可以看看[這個](http://stackoverflow.com/questions/2375222/java-simpledateformat-for-time-zone-with-a-colon-分隔符)討論你的問題,甚至[this](http://stackoverflow.com/questions/2201925/converting-iso8601-compliant-string-to-java-util-date) – MadProgrammer 2013-04-10 00:34:30
是的,我可以在TextMate中替換它們,時區在所有記錄中應該是相同的,因爲所有數據都是從同一個地方寫入的。謝謝,我會試試這個。 – 2013-04-10 00:47:11