Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get date in "yyyy-MM-dd HH:mm:ss" format properly

Tags:

android

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.

like image 697
user3782779 Avatar asked Mar 14 '23 19:03

user3782779


2 Answers

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)

like image 55
ArtKorchagin Avatar answered Apr 01 '23 09:04

ArtKorchagin


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.

like image 20
Sandeep Manmode Avatar answered Apr 01 '23 08:04

Sandeep Manmode