Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <string name="date_time_format">dd MMMM yyyy, hh.mm a</string>
- <string name="date_time_format">dd.MM.yyyy, HH:mm</string>
- SimpleDateFormat fmtOut = new SimpleDateFormat(context.getString(R.string.date_time_format), Locale.getDefault());
- <string name="date_time_string">%s</string>
- <string name="date_time_string">%s Uhr</string>
- return String.format(context.getString(R.string.date_time_string), fmtOut.format(date));
- java.sql.Date date1 = new java.sql.Date((new Date()).getTime());
- SimpleDateFormat formatNowDay = new SimpleDateFormat("dd");
- SimpleDateFormat formatNowYear = new SimpleDateFormat("yyyy");
- SimpleDateFormat sdf12 = new SimpleDateFormat("hh:mm aa");
- SimpleDateFormat sdf24 = new SimpleDateFormat("HH:mm");
- SimpleDateFormat germanSdf = new SimpleDateFormat("dd.MM.yyyy");
- String currentDay = formatNowDay.format(date1);
- String currentYear = formatNowYear.format(date1);
- String time12 = sdf12.format(date1);
- String time24 = sdf24.format(date1);
- String germanTime = germanSdf.format(date1);
- Calendar mCalendar = Calendar.getInstance();
- String currentMonth = mCalendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
- String engTime = currentDay+" "+currentMonth+" "+currentYear+", "+time12;
- String germanFormat = germanTime+", "+time24+" Uhr";
- DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.ENGLISH);
- // get device's locale
- Locale locale = Locale.getDefault();
- String pattern = "";
- // check language
- if ("en".equals(locale.getLanguage())) {
- // english
- pattern = "dd MMMM yyyy, h.mm a";
- } else if ("de".equals(locale.getLanguage())) {
- // german
- pattern = "dd.MM.yyyy, HH:mm 'Uhr'";
- } else {
- pattern = // use some default pattern for other languages?
- }
- SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);
- String formattedDate = sdf.format(new Date());
- // change AM/PM to am/pm (only for English)
- if ("en".equals(locale.getLanguage())) {
- formattedDate = formattedDate.toLowerCase();
- }
- Locale locale = Locale.getDefault();
- FormatStyle style = FormatStyle.MEDIUM;
- String pattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(style, style, IsoChronology.INSTANCE, locale);
- DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern, locale);
- // get device's locale
- Locale locale = Locale.getDefault();
- String pattern = "";
- // check language
- if ("en".equals(locale.getLanguage())) {
- // english
- pattern = "dd MMMM yyyy, h.mm a";
- } else if ("de".equals(locale.getLanguage())) {
- // german
- pattern = "dd.MM.yyyy, HH:mm 'Uhr'";
- } else {
- pattern = ""; // use some default pattern?
- }
- DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern, locale);
- String formattedDate = LocalDateTime.now().format(fmt);
- // map of formatters
- Map<String, DateTimeFormatter> formatterMap = new HashMap<>();
- // English formatter
- Map<Long, String> customAmPmSymbols = new HashMap<>();
- customAmPmSymbols.put(0L, "am");
- customAmPmSymbols.put(1L, "pm");
- DateTimeFormatter f = new DateTimeFormatterBuilder()
- // date/time
- .appendPattern("dd MMMM yyyy, h.mm ")
- // custom AM/PM symbols (lowercase)
- .appendText(ChronoField.AMPM_OF_DAY, customAmPmSymbols)
- // create formatter
- .toFormatter(Locale.ENGLISH);
- // add to map
- formatterMap.put("en", f);
- // German formatter
- formatterMap.put("de", DateTimeFormatter.ofPattern("dd.MM.yyyy, HH:mm 'Uhr'", Locale.GERMAN));
- // get device's locale
- Locale locale = Locale.getDefault();
- DateTimeFormatter fmt = formatterMap.get(locale.getLanguage());
- if (fmt != null) {
- String formattedDate = LocalDateTime.now().format(fmt);
- }
Add Comment
Please, Sign In to add comment