Steve Rogers Steve Rogers - 4 years ago 95
Android Question

Get string date from millis with API level 15 with right Locale and 24h

I stored a Date in sqlite db using INTEGER this:

System.currentTimeMillis()


And now i'm getting it back into a LONG and after to string with this:

myTextView.setText(convertDate(date_in_millis.toString(),"dd/MM/yyyy hh:mm:ss");


using this function

public static String convertDate(String dateInMilliseconds, String dateFormat) {
return DateFormat.format(dateFormat, Long.parseLong(dateInMilliseconds)).toString();
}


I'm in Italy.

Well, first of all i'm losing somewhere hours and minutes.

Second i'm not able to set 24h (with HH instead of hh)

I need to use API level 15 then i cannot use API 24 Libraries and i'm unable to use most answers i found.

Answer Source

Try using the Calendar class:

public static String convertDate(String dateInMilliseconds, String dateFormat) {
DateFormat df = new SimpleDateFormat(dateFormat);
Calendar c = Calendar.getInstance();
c.setTimeInMillis(Long.parseLong(dateInMilliseconds));
cal.add(Calendar.HOUR_OF_DAY, 1); //Work-around to get your lost hour
return df.format(c.getTime());
}

And you can use HH to get 24h

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download