4
String date = (String) android.text.format.DateFormat.format("yyyy-MM-dd HH:mm:ss", new java.util.Date());

Is some device work ok. But in other devices I get:

2015-12-24 HH:28:24

In devices that not work if I change (HH) - > (hh) works. But I need hour in 24 hour mode.

user3782779
  • 1,669
  • 3
  • 22
  • 36
  • 1
    used `SimpleDateFormat` instead of `DateFormat`. check [This Post](http://stackoverflow.com/questions/28559224/display-date-in-24-hour-issue) – M D Dec 24 '15 at 10:29
  • Check my [this answer](http://stackoverflow.com/a/33799613/2553431) – Iamat8 Dec 24 '15 at 10:30

2 Answers2

6

Try to use kk instead of hh for instance:

String date = (String) android.text.format.DateFormat.format("yyyy-MM-dd kk:mm:ss", new java.util.Date());

Or use SimpleDateFormat:

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatter.format(new Date(System.currentTimeMillis()));

Check out documentation: SimpleDateFormat

H - Hour in day (0-23)

k - Hour in day (1-24)

K - Hour in am/pm (0-11)

h - Hour in am/pm (1-12)

ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58
1

You can use as:

    Date dt = new Date();

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");



    String check = dateFormat.format(dt);


    System.out.println("DATE_TO_FROM_DATEBASE   " + 
            arrayOfStringDate[d].toString());
    System.out.println("CURRENT_DATE   " + check);

Hope so this may help you.

Sandeep Manmode
  • 395
  • 4
  • 15