fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.text.DateFormat;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.ArrayList;
  7. import java.util.Date;
  8. import java.util.List;
  9. import java.util.Locale;
  10. import java.util.SimpleTimeZone;
  11. import java.util.TimeZone;
  12.  
  13. /* The class name doesn't have to be Main, as long as the class is not public. */
  14. class Main
  15. {
  16.  
  17. public static final ThreadLocal<SimpleDateFormat> TFS_DATETIME_FORMATTER = new ThreadLocal<SimpleDateFormat>() {
  18. @Override
  19. protected SimpleDateFormat initialValue() {
  20. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
  21. dateFormat.setTimeZone(new SimpleTimeZone(0,"GMT"));
  22. return dateFormat;
  23. }
  24. };
  25.  
  26. public static Date parseDate(String dateString) throws ParseException {
  27. return parseDate(dateString, Locale.getDefault(), TimeZone.getDefault());
  28. }
  29.  
  30. @SuppressWarnings("deprecation")
  31. public static Date parseDate(String dateString, Locale locale, TimeZone timezone) throws ParseException {
  32. Date date = null;
  33. dateString = dateString.replaceAll("(p|P)\\.(m|M)\\.", "PM").replaceAll("(a|A)\\.(m|M)\\.", "AM");
  34. try {
  35. // Use the deprecated Date.parse method as this is very good at detecting
  36. // dates commonly output by the US and UK standard locales of dotnet that
  37. // are output by the Microsoft command line client.
  38. date = new Date(Date.parse(dateString));
  39. // ignore - parse failed.
  40. }
  41. if (date == null) {
  42. // The old fashioned way did not work. Let's try it using a more
  43. // complex alternative.
  44. //DateFormat[] formats = createDateFormatsForLocaleAndTimeZone(locale, timezone);
  45. DateFormat[] formats = createDateFormatsForLocaleAndTimeZone(locale, timezone);
  46. return parseWithFormats(dateString, formats);
  47. }
  48. return date;
  49. }
  50.  
  51. public static Date parseWithFormats(String input, DateFormat[] formats) throws ParseException {
  52. ParseException parseException = null;
  53. for (int i = 0; i < formats.length; i++) {
  54. try {
  55. return formats[i].parse(input);
  56. } catch (ParseException ex) {
  57. parseException = ex;
  58. }
  59. }
  60. if (parseException == null) {
  61. throw new IllegalStateException("No dateformats found that can be used for parsing '" + input + "'");
  62. }
  63. throw parseException;
  64. }
  65.  
  66. /**
  67. * Build an array of DateFormats that are commonly used for this locale
  68. * and timezone.
  69. */
  70. public static DateFormat[] createDateFormatsForLocaleAndTimeZone(Locale locale, TimeZone timeZone) {
  71. List<DateFormat> formats = new ArrayList<DateFormat>();
  72.  
  73. addDateTimeFormatsToList(locale, timeZone, formats);
  74. addDateFormatsToList(locale, timeZone, formats);
  75.  
  76. return formats.toArray(new DateFormat[formats.size()]);
  77. }
  78.  
  79. public static void addDateFormatsToList(Locale locale, TimeZone timeZone, List<DateFormat> formats) {
  80. for (int dateStyle = DateFormat.FULL; dateStyle <= DateFormat.SHORT; dateStyle++) {
  81. DateFormat df = DateFormat.getDateInstance(dateStyle, locale);
  82. df.setTimeZone(timeZone);
  83. formats.add(df);
  84. }
  85. }
  86.  
  87. public static void addDateTimeFormatsToList(Locale locale, TimeZone timeZone, List<DateFormat> formats) {
  88. for (int dateStyle = DateFormat.FULL; dateStyle <= DateFormat.SHORT; dateStyle++) {
  89. for (int timeStyle = DateFormat.FULL; timeStyle <= DateFormat.SHORT; timeStyle++) {
  90. DateFormat df = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
  91. if (timeZone != null) {
  92. df.setTimeZone(timeZone);
  93. }
  94. formats.add(df);
  95. }
  96. }
  97. }
  98.  
  99. public static void main (String[] args) throws java.lang.Exception
  100. {
  101. String dateString = new String("13. februar 2013 14:28:33");
  102. //Locale locale = Locale.getDefault();
  103. Locale locale = new Locale("nb");
  104. TimeZone timezone = TimeZone.getDefault();
  105.  
  106. //Date date2 = parseDate(dateString);
  107.  
  108.  
  109. java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
  110. String str;
  111. while (!(str=r.readLine()).startsWith("42")) System.out.println(str);
  112.  
  113.  
  114. Locale[] locales = {Locale.UK, Locale.FRANCE, Locale.ITALIAN, new Locale("nb")};
  115. Date date = new Date();
  116. String s;
  117. for (int i = 0; i < locales.length; i++) {
  118. s = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM, locales[i]).format(date);
  119. System.out.println(locales[i].getDisplayLanguage() + "\t : " + s);
  120. }
  121.  
  122. DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM, new Locale("nb"));
  123. df.parse("13. februar 2013 14:28:33");
  124.  
  125. Date date3 = parseDate(dateString, locale, timezone);
  126.  
  127. System.out.println(DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM, new Locale("en")).format(date3));
  128. }
  129. }
Success #stdin #stdout 0.12s 382080KB
stdin
1
2
10
42
11
stdout
1
2
10
English	 : Thursday, 21 February 2013 19:12:59
French	 : jeudi 21 février 2013 19:12:59
Italian	 : giovedì 21 febbraio 2013 19.12.59
Norwegian Bokmål	 : 21. februar 2013 19:12:59
Wednesday, February 13, 2013 2:28:33 PM