Formatting date and time according to user language and preferences with seconds

I am trying to get a formatted date (year, month, date) and time (hour, minute, second) according to user settings. This post in the Google Android development team describes the exact problem I encountered, but no one helped this person solve this problem. I will summarize:

Android has these classes that are trying to do the above, but none use both user preferences and shows seconds.

  • java.text.DateFormat
    Does not use the settings set in the Settings application

  • android.text.format.DateFormat
    Gets an object java.text.DateFormatthat correctly formats the output using getDateFormat()and getTimeFormat(), but getTimeFormat()does not include seconds.

  • android.text.format.DateUtils
    Does not use the settings set to display the date in the Settings application, and there is no way to display seconds.

For example, with the settings in the settings set for DD / MM / YYYY and 24-hour format = on, the following code is executed:

long timestamp=System.currentTimeMillis();

sometextview.setText(
    java.text.format.DateFormat.getDateTimeInstance().format(new Date(timestamp)) 

    +"\n"+ 

    android.text.format.DateFormat.getDateFormat(this).format(new Date(timestamp))
    +" "+
    android.text.format.DateFormat.getTimeFormat(this).format(new Date(timestamp))

    +"\n"+

    android.text.format.DateUtils.formatDateTime(this,
                                                 timestamp,    
                                                 DateUtils.FORMAT_SHOW_DATE| 
                                                 DateUtils.FORMAT_SHOW_TIME|
                                                 DateUtils.FORMAT_SHOW_YEAR)
);

gives me the following output in text form:

Apr 27,2014 5:47:18 PM
27/04/2014 17:47
April 27,2014, 17:47

None gives the desired result, which would be like this using the above settings:

27/04/2014 17:47:18 

I looked through the joda-time-android library , but it doesn't seem to do what I need (correct me if I'm wrong).

TL; DR: / Android?

+4
5

@I wish I could think of a good, , :

public static String timeDateStringFromTimestamp(Context applicationContext,long timestamp){
    String timeDate;
    String androidDateTime=android.text.format.DateFormat.getDateFormat(applicationContext).format(new Date(timestamp))+" "+
            android.text.format.DateFormat.getTimeFormat(applicationContext).format(new Date(timestamp));
    String javaDateTime = DateFormat.getDateTimeInstance().format(new Date(timestamp));
    String AmPm="";
    if(!Character.isDigit(androidDateTime.charAt(androidDateTime.length()-1))) {
        if(androidDateTime.contains(new SimpleDateFormat().getDateFormatSymbols().getAmPmStrings()[Calendar.AM])){
            AmPm=" "+new SimpleDateFormat().getDateFormatSymbols().getAmPmStrings()[Calendar.AM];
        }else{
            AmPm=" "+new SimpleDateFormat().getDateFormatSymbols().getAmPmStrings()[Calendar.PM];
        }
        androidDateTime=androidDateTime.replace(AmPm, "");
    }
    if(!Character.isDigit(javaDateTime.charAt(javaDateTime.length()-1))){
        javaDateTime=javaDateTime.replace(" "+new SimpleDateFormat().getDateFormatSymbols().getAmPmStrings()[Calendar.AM], "");
        javaDateTime=javaDateTime.replace(" "+new SimpleDateFormat().getDateFormatSymbols().getAmPmStrings()[Calendar.PM], "");
    }
    javaDateTime=javaDateTime.substring(javaDateTime.length()-3);
    timeDate=androidDateTime.concat(javaDateTime);
    return timeDate.concat(AmPm);
}
+4

:

long timestamp = System.currentTimeMillis();

DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
DateFormat timeFormat = DateFormat.getTimeInstance();

String currentTimeString =  dateFormat.format(timestamp) + " - " +            
       timeFormat.format(timestamp);
+4

android.text.format.DateFormat.getDateFormat(this).format(new Date(timestamp));

java.text.format.DateFormat.getDateTimeInstance().format(new Date(timestamp))

getDateTimeInstance , getDateFormat

+3

TL;DR

ZonedDateTime.now( ZoneId.of( "America/Montreal" ) )
             .format( DateTimeFormatter.ofPattern( "dd/MM/uuuu HH:mm:ss" ) )

23/01/2017 12:34:56

java.time

ThreeTenABP (. ), java.time. -, , , .

. . , - , "" .

continent/region, America/Montreal, Africa/Casablanca Pacific/Auckland. 3-4 , EST IST, , (!).

ZoneId z = ZoneId.of( "America/Montreal" );  // Or ZoneId.systemDefault()
ZonedDateTime zdt = ZonedDateTime.now( z );

DateTimeFormatter, , ZonedDateTime. toLocalized…, .

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu HH:mm:ss" ) ; 
String output = zdt.format( f );

23/01/2017 12:34:56


java.time

java.time Java 8 . legacy , java.util.Date, Calendar SimpleDateFormat.

Joda-Time, , java.time.

, . Oracle. Qaru . JSR 310.

java.time?


Joda

: Joda-Time, , java.time.

Joda-Time . DateTimeFormat .

DateTimeFormatter formatter = DateTimeFormat.forStyle( "SM" );
DateTime dateTime = DateTime.now();
String output = formatter.print( dateTime );

, . - , - . "S" , "M" , "L" "F" . "-".

Locale

A Locale , , .

Locale, JVM . , / . , , Locale.getDefault, , .

formatter = formatter.withLocale( Locale.CANADA_FRENCH );
0

, :

Date date = ...;
String dateStr = DateFormat.getLongDateFormat(context).format(date);
String timeStr;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
  String timeFormat = DateFormat.getBestDateTimePattern(Locale.getDefault(), DateFormat.is24HourFormat(context) ? "Hms" : "hmsa");
  timeStr = new SimpleDateFormat(timeFormat, Locale.getDefault()).format(date);
}
else {
  String timeFormat = DateFormat_getTimeFormatString(context).replace("mm", "mm:ss");
  timeStr = new SimpleDateFormat(timeFormat, Locale.getDefault()).format(date);
}
return dateStr + " " + timeStr;

"DateFormat", :

private static String DateFormat_getTimeFormatString(Context context) {
  try {
    final Method method = DateFormat.class.getDeclaredMethod("getTimeFormatString");
    method.setAccessible(true);
    return (String) method.invoke(context);
  }
  catch (Exception ignored) {
    return "HH:mm";
  }
}

API 18, getBestDateTimePattern(), , , .

is24HourFormat(), ( H ), , , .

, , , DateUtils.formatDateTime().

. , is24HourFormat(). .

0

Source: https://habr.com/ru/post/1538336/


All Articles