Guest User

Untitled

a guest
Mar 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. <string name="date_time_format">dd MMMM yyyy, hh.mm a</string>
  2. <string name="date_time_format">dd.MM.yyyy, HH:mm</string>
  3.  
  4. SimpleDateFormat fmtOut = new SimpleDateFormat(context.getString(R.string.date_time_format), Locale.getDefault());
  5.  
  6. <string name="date_time_string">%s</string>
  7. <string name="date_time_string">%s Uhr</string>
  8.  
  9. return String.format(context.getString(R.string.date_time_string), fmtOut.format(date));
  10.  
  11. java.sql.Date date1 = new java.sql.Date((new Date()).getTime());
  12.  
  13. SimpleDateFormat formatNowDay = new SimpleDateFormat("dd");
  14. SimpleDateFormat formatNowYear = new SimpleDateFormat("yyyy");
  15. SimpleDateFormat sdf12 = new SimpleDateFormat("hh:mm aa");
  16. SimpleDateFormat sdf24 = new SimpleDateFormat("HH:mm");
  17. SimpleDateFormat germanSdf = new SimpleDateFormat("dd.MM.yyyy");
  18.  
  19. String currentDay = formatNowDay.format(date1);
  20. String currentYear = formatNowYear.format(date1);
  21. String time12 = sdf12.format(date1);
  22. String time24 = sdf24.format(date1);
  23.  
  24. String germanTime = germanSdf.format(date1);
  25.  
  26. Calendar mCalendar = Calendar.getInstance();
  27. String currentMonth = mCalendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
  28.  
  29. String engTime = currentDay+" "+currentMonth+" "+currentYear+", "+time12;
  30. String germanFormat = germanTime+", "+time24+" Uhr";
  31.  
  32. DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.ENGLISH);
  33.  
  34. // get device's locale
  35. Locale locale = Locale.getDefault();
  36.  
  37. String pattern = "";
  38. // check language
  39. if ("en".equals(locale.getLanguage())) {
  40. // english
  41. pattern = "dd MMMM yyyy, h.mm a";
  42. } else if ("de".equals(locale.getLanguage())) {
  43. // german
  44. pattern = "dd.MM.yyyy, HH:mm 'Uhr'";
  45. } else {
  46. pattern = // use some default pattern for other languages?
  47. }
  48.  
  49. SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);
  50. String formattedDate = sdf.format(new Date());
  51.  
  52. // change AM/PM to am/pm (only for English)
  53. if ("en".equals(locale.getLanguage())) {
  54. formattedDate = formattedDate.toLowerCase();
  55. }
  56.  
  57. Locale locale = Locale.getDefault();
  58. FormatStyle style = FormatStyle.MEDIUM;
  59. String pattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(style, style, IsoChronology.INSTANCE, locale);
  60. DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern, locale);
  61.  
  62. // get device's locale
  63. Locale locale = Locale.getDefault();
  64.  
  65. String pattern = "";
  66. // check language
  67. if ("en".equals(locale.getLanguage())) {
  68. // english
  69. pattern = "dd MMMM yyyy, h.mm a";
  70. } else if ("de".equals(locale.getLanguage())) {
  71. // german
  72. pattern = "dd.MM.yyyy, HH:mm 'Uhr'";
  73. } else {
  74. pattern = ""; // use some default pattern?
  75. }
  76.  
  77. DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern, locale);
  78.  
  79. String formattedDate = LocalDateTime.now().format(fmt);
  80.  
  81. // map of formatters
  82. Map<String, DateTimeFormatter> formatterMap = new HashMap<>();
  83.  
  84. // English formatter
  85. Map<Long, String> customAmPmSymbols = new HashMap<>();
  86. customAmPmSymbols.put(0L, "am");
  87. customAmPmSymbols.put(1L, "pm");
  88. DateTimeFormatter f = new DateTimeFormatterBuilder()
  89. // date/time
  90. .appendPattern("dd MMMM yyyy, h.mm ")
  91. // custom AM/PM symbols (lowercase)
  92. .appendText(ChronoField.AMPM_OF_DAY, customAmPmSymbols)
  93. // create formatter
  94. .toFormatter(Locale.ENGLISH);
  95. // add to map
  96. formatterMap.put("en", f);
  97.  
  98. // German formatter
  99. formatterMap.put("de", DateTimeFormatter.ofPattern("dd.MM.yyyy, HH:mm 'Uhr'", Locale.GERMAN));
  100.  
  101. // get device's locale
  102. Locale locale = Locale.getDefault();
  103.  
  104. DateTimeFormatter fmt = formatterMap.get(locale.getLanguage());
  105.  
  106. if (fmt != null) {
  107. String formattedDate = LocalDateTime.now().format(fmt);
  108. }
Add Comment
Please, Sign In to add comment