By pupeno

2009-01-17 23:41:37 8 Comments

How to format correctly according to the device configuration date and time when having a year, month, day, hour and minute?


@Sunil 2019-11-17 14:37:33

Date format class work with cheat code to make date. Like

  1. M -> 7, MM -> 07, MMM -> Jul , MMMM -> July
  2. EEE -> Tue , EEEE -> Tuesday
  3. z -> EST , zzz -> EST , zzzz -> Eastern Standard Time

You can check more cheats here.

@k8C 2019-03-29 09:11:31

Shortest way:

// 2019-03-29 16:11
String.format("%1$tY-%<tm-%<td %<tR", Calendar.getInstance())

%tR is short for %tH:%tM, < means to reuse last parameter(1$).

It is equivalent to String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM", Calendar.getInstance())

@Dany Pop 2017-11-03 15:09:32

You can use DateFormat. Result depends on default Locale of the phone, but you can specify Locale too :

This is results on a


FR Locale : 3 nov. 2017

US/En Locale : Jan 12, 1952


FR Locale : 03/11/2017

US/En Locale : 12.13.52


FR Locale : 3 nov. 2017

US/En Locale : Jan 12, 1952


FR Locale : 3 novembre 2017

US/En Locale : January 12, 1952


FR Locale : vendredi 3 novembre 2017

US/En Locale : Tuesday, April 12, 1952


FR Locale : 3 nov. 2017 16:04:58

DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date)

FR Locale : 03/11/2017 16:04

DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(date)

FR Locale : 03/11/2017 16:04:58

DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG).format(date)

FR Locale : 03/11/2017 16:04:58 GMT+01:00

DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.FULL).format(date)

FR Locale : 03/11/2017 16:04:58 heure normale d’Europe centrale


FR Locale : 16:04:58


FR Locale : 16:04


FR Locale : 16:04:58


FR Locale : 16:04:58 GMT+01:00


FR Locale : 16:04:58 heure normale d’Europe centrale

@zeeshan 2017-11-21 19:05:44

Thank you for putting all these cases in one place. If you could add cases for time only as well, this will make it a complete reference.

@Opriday 2017-10-08 18:04:24

This code work for me!

Date d = new Date();
    CharSequence s = android.text.format.DateFormat.format("MM-dd-yy hh-mm-ss",d.getTime());

@Wojtek 2017-09-26 19:05:21


To get date or time in locale format from milliseconds I used this:

Date and time

Date date = new Date(milliseconds);
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.getDefault());


Date date = new Date(milliseconds);
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault());


Date date = new Date(milliseconds);
DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());

You can use other date style and time style. More info about styles here.

@Ole V.V. 2017-09-08 09:03:20

The other answers are generally correct. I should like to contribute the modern answer. The classes Date, DateFormat and SimpleDateFormat used in most of the other answers, are long outdated and have caused trouble for many programmers over many years. Today we have so much better in java.time, AKA JSR-310, the modern Java date & time API. Can you use this on Android yet? Most certainly! The modern classes have been backported to Android in the ThreeTenABP project. See this question: How to use ThreeTenABP in Android Project for all the details.

This snippet should get you started:

    int year = 2017, month = 9, day = 28, hour = 22, minute = 45;
    LocalDateTime dateTime = LocalDateTime.of(year, month, day, hour, minute);
    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);

When I set my computer’s preferred language to US English or UK English, this prints:

Sep 28, 2017 10:45:00 PM

When instead I set it to Danish, I get:

28-09-2017 22:45:00

So it does follow the configuration. I am unsure exactly to what detail it follows your device’s date and time settings, though, and this may vary from phone to phone.

@Tomas 2015-01-13 10:55:46


I use SimpleDateFormat without custom pattern to get actual date and time from the system in the device's preselected format:

public static String getFormattedDate() {
    //SimpleDateFormat called without pattern
    return new SimpleDateFormat().format(Calendar.getInstance().getTime());


  • 13.01.15 11:45
  • 1/13/15 10:45 AM
  • ...

@Fuangwith S. 2010-10-14 04:34:50

In my opinion, android.text.format.DateFormat.getDateFormat(context) makes me confused because this method returns java.text.DateFormat rather than android.text.format.DateFormat - -".

So, I use the fragment code as below to get the current date/time in my format.

android.text.format.DateFormat df = new android.text.format.DateFormat();
df.format("yyyy-MM-dd hh:mm:ss a", new java.util.Date());


android.text.format.DateFormat.format("yyyy-MM-dd hh:mm:ss a", new java.util.Date());

In addition, you can use others formats. Follow DateFormat.

@Chris Boyle 2010-10-14 16:26:48

Useful, but the question said "according to the device configuration". To me that implies using a format chosen based on the user's language/country, or chosen directly by the user, rather than hardcoding the choice of format.

@dnet 2012-04-17 11:51:58

also, don't forget that hh:mm:ss will give you 01:00:00 for 1 PM, you'll need to use kk:mm:ss to get 13:00:00

@Joony 2012-12-07 13:25:22

@dnet k is hour in day (1-24), do you not mean H, which is hour in day (0-23), eg. HH:mm:ss? See:‌​tml

@dnet 2012-12-07 22:51:19

@Joony no, there's difference between java.text.SimpleDateFormat (what you linked and uses H for hours in the 0-23 range) and android.text.format.DateFormat (what the answer is about and uses k for hours in the 0-23 range)

@Joony 2012-12-10 11:24:19

@dnet After testing, you are correct about k, however, the documentation for DateFormat clearly states For the canonical documentation of format strings, see SimpleDateFormat. Very confusing. Or am I missing something?

@Muthukannan Kanniappan 2013-12-05 14:15:38

Seems like format is a static method in android.text.format.DateFormat.

@TWiStErRob 2014-12-20 00:50:41

@Joony I see your confusion and I had a huge WTF moment, see this commit for android...DateFormat, but it seems SimpleDateFormat works as intended.

@Borzh 2015-06-17 15:14:24

You can use also android.text.format.DateFormat.getLongDateFormat().

@alexpfx 2016-11-28 17:51:27

Back to 2016, When I want to customize the format (not according to the device configuration, as you ask...) I usually use the string resource file:

in strings.xml:

<string name="myDateFormat"><xliff:g id="myDateFormat">%1$td/%1$tm/%1$tY</xliff:g></string>

In Activity:

Log.d(TAG, "my custom date format: "+getString(R.string.myDateFormat, new Date()));

This is also useful with the release of the new Date Binding Library.

So I can have something like this in layout file:


And in java class:

    MovieDetailViewModel vm = new MovieDetailViewModel();
    vm.setReleaseDate(new Date());

@tronman 2010-08-03 21:48:07

This will do it:

Date date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date));

@Kannan_SJD 2016-07-01 07:16:45

Will this get localized when user changes the language in an android device?

@subrahmanyam boyapati 2016-10-12 10:38:36

It's too late but it may help to someone

DateFormat.format(format, timeInMillis);

here format is what format you need

ex: "HH:mm" returns 15:30

@Ivo Stoyanov 2015-06-10 11:24:33

Date to Locale date string:

Date date = new Date();
String stringDate = DateFormat.getDateTimeInstance().format(date);



- > Dec 31, 1969


-> Dec 31, 1969 4:00:00 PM


-> 4:00:00 PM

@Hardik Joshi 2015-11-08 17:36:53

How to remove year from DateFormat.getDateInstance() ?

@Basil Bourque 2014-08-08 19:41:00

Avoid j.u.Date

The Java.util.Date and .Calendar and SimpleDateFormat in Java (and Android) are notoriously troublesome. Avoid them. They are so bad that Sun/Oracle gave up on them, supplanting them with the new java.time package in Java 8 (not in Android as of 2014). The new java.time was inspired by the Joda-Time library.


Joda-Time does work in Android.

Search StackOverflow for "Joda" to find many examples and much discussion.

A tidbit of source code using Joda-Time 2.4.

Standard format.

String output =; 
// Current date-time in user's default time zone with a String representation formatted to the ISO 8601 standard.

Localized format.

String output = DateTimeFormat.forStyle( "FF" ).print( ); 
// Full (long) format localized for this user's language and culture.

@Jorgesys 2014-04-11 17:54:27

This is my method, you can define and input and output format.

public static String formattedDateFromString(String inputFormat, String outputFormat, String inputDate){
    if(inputFormat.equals("")){ // if inputFormat = "", set a default input format.
        inputFormat = "yyyy-MM-dd hh:mm:ss";
        outputFormat = "EEEE d 'de' MMMM 'del' yyyy"; // if inputFormat = "", set a default output format.
    Date parsed = null;
    String outputDate = "";

    SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, java.util.Locale.getDefault());
    SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, java.util.Locale.getDefault());

    // You can set a different Locale, This example set a locale of Country Mexico.
    //SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, new Locale("es", "MX"));
    //SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, new Locale("es", "MX"));

    try {
        parsed = df_input.parse(inputDate);
        outputDate = df_output.format(parsed);
    } catch (Exception e) { 
        Log.e("formattedDateFromString", "Exception in formateDateFromstring(): " + e.getMessage());
    return outputDate;


@FireZenk 2014-01-05 16:50:00

Following this:

Is better to use Android native Time class:

Time now = new Time();

Then format:

Log.d("DEBUG", "Time "+now.format("%d.%m.%Y %H.%M.%S"));

@ccpizza 2015-01-01 14:30:05

@FireZenk: According to the [link](‌​l) you provided: This class has a number of issues and it is recommended that GregorianCalendar is used instead.

@FireZenk 2015-01-02 14:59:58

Oh... this issue-info is newer than my comment, so that's a deprecated answer

@ViliusK 2013-11-06 22:15:35

I use it like this:

public class DateUtils {
    static DateUtils instance;
    private final DateFormat dateFormat;
    private final DateFormat timeFormat;

    private DateUtils() {
        dateFormat = android.text.format.DateFormat.getDateFormat(MainApplication.context);
        timeFormat = android.text.format.DateFormat.getTimeFormat(MainApplication.context);

    public static DateUtils getInstance() {
        if (instance == null) {
            instance = new DateUtils();
        return instance;

    public synchronized static String formatDateTime(long timestamp) {
        long milliseconds = timestamp * 1000;
        Date dateTime = new Date(milliseconds);
        String date = getInstance().dateFormat.format(dateTime);
        String time = getInstance().timeFormat.format(dateTime);
        return date + " " + time;

@Broo 2013-08-30 09:22:21

The android Time class provides 3 formatting methods

This is how I did it:

* This method will format the data from the android Time class (eg. myTime.setToNow())   into the format
* Date: Time:
private String formatTime(String time)
    String fullTime= "";
    String[] sa = new String[2];

        Time t = new Time(Time.getCurrentTimezone());
        // or t.setToNow();
        String formattedTime = t.format("%d.%m.%Y %H.%M.%S");
        int x = 0;

        for(String s : formattedTime.split("\\s",2))
            System.out.println("Value = " + s);
            sa[x] = s;
        fullTime = "Date: " + sa[0] + " Time: " + sa[1];
        fullTime = "No time data";
    return fullTime;

I hope thats helpful :-)

@Igor Krumpak 2013-01-16 13:10:03

Use build in Time class!

Time time = new Time();
time.set(0, 0, 17, 4, 5, 1999);
Log.i("DateTime", time.format("%d.%m.%Y %H:%M:%S"));

@Dean Wild 2013-01-29 12:17:52

This is the optimal solution because it uses the lightweight Time object from the Android Framework:‌​l

@Dan Hulme 2013-04-25 16:47:15

This is not the optimal solution: it doesn't respect the date format from the user's locale.

@neknek mouh 2012-12-18 07:55:19

Use SimpleDateFormat

Like this:

event.putExtra("starttime", "12/18/2012");

SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Date date = format.parse(bundle.getString("starttime"));

@iutinvg 2013-04-10 05:33:39

Yes, with default locale to avoid performance issues: new SimpleDateFormat("my-format", Locale.getDefault());

@user1367623 2012-06-23 05:44:02

Use these two as a class variables:

 public java.text.DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
 private Calendar mDate = null;

And use it like this:

 mDate = Calendar.getInstance();

@Abhishek Singh Rathaur 2012-03-26 06:46:30


event.putExtra("startTime", "10/05/2012");

And when you are accessing passed variables:

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(bundle.getString("startTime"));

@chetan 2011-07-27 10:39:17

This code would return the current date and time:

public String getCurrDate()
    String dt;
    Date cal = Calendar.getInstance().getTime();
    dt = cal.toLocaleString();
    return dt;

@Kopfgeldjaeger 2013-03-28 17:13:42

toLocaleString() is deprecated

@JamieH 2009-01-19 08:57:09

Use the standard Java DateFormat class.

For example to display the current date and time do the following:

Date date = new Date(location.getTime());
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText("Time: " + dateFormat.format(date));

You can initialise a Date object with your own values, however you should be aware that the constructors have been deprecated and you should really be using a Java Calendar object.

@jamesh 2009-09-07 23:31:20

This is the android.text.format.DateFormat rather than java.text.DateFormat.

@mxcl 2010-07-20 14:14:34

It's pretty typical of Android IME to have two classes that both claim to give you a result that is set to the default Locale but one doesn't. So yes, don't forget to use the android.text.format version of DateFormat (that doesn't even derive the java.util one LOL).

@Harsha M V 2010-12-27 12:30:26

am having problem with what i should import - i mean lib files :(

@Arye Rosenstein 2011-02-13 06:53:27

Please note this line: DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationC‌​ontext()); The returned dateFormat is of type java.text.DateFormat (and NOT android.text.format.DateFormat)

@Jerry Brady 2011-08-22 19:06:16

@Harsha - to get around that issue, I chain my use of DateFormat so I only have to reference the Android class and therefore there aren't any ambiguous classes. final String dateStr = DateFormat.getDateFormat(this).format(d); You can use Android's format() method and have (IMHO) cleaner code and one less Object to instantiate.

@Asmo Soinio 2011-12-02 13:43:44

This formatter only includes the date, not the time as the original question stated. Use DateUtils from the same package instead, see…

@RobCroll 2012-12-26 18:25:49

Unfortunately this may not work on all devices. I'm testing with a HTC desire with "Select date format" in settings as ddd, MMM dd, yyyy and the dateformatter is returning MM/dd/yy!

@allsoft 2018-05-30 14:47:20

For a full format, use an api. android.text.format.DateFormat.getLongDateFormat(Context);

@ThomasW 2018-11-21 08:10:54

@JerryBrady your way is cleaner, you may want to consider writing it up as a separate answer.

Related Questions

Sponsored Content

39 Answered Questions

[SOLVED] How to get current time and date in Android

  • 2011-03-20 16:12:25
  • M7M
  • 1371626 View
  • 1082 Score
  • 39 Answer
  • Tags:   android date time

9 Answered Questions

[SOLVED] Why is subtracting these two times (in 1927) giving a strange result?

  • 2011-07-27 08:15:58
  • Freewind
  • 643707 View
  • 6737 Score
  • 9 Answer
  • Tags:   java date timezone

104 Answered Questions

[SOLVED] Close/hide android soft keyboard

53 Answered Questions

[SOLVED] How do I get the current date in JavaScript?

  • 2009-10-07 11:39:02
  • Suresh Chaganti
  • 2603105 View
  • 2234 Score
  • 53 Answer
  • Tags:   javascript date

55 Answered Questions

[SOLVED] How to format a JavaScript date

36 Answered Questions

[SOLVED] How to get the current time in Python

  • 2009-01-06 04:54:23
  • user46646
  • 3178311 View
  • 2734 Score
  • 36 Answer
  • Tags:   python datetime time

14 Answered Questions

[SOLVED] Java string to date conversion

38 Answered Questions

[SOLVED] How do you get a timestamp in JavaScript?

40 Answered Questions

[SOLVED] Detecting an "invalid date" Date instance in JavaScript

  • 2009-08-30 11:34:40
  • orip
  • 781589 View
  • 1426 Score
  • 40 Answer
  • Tags:   javascript date

37 Answered Questions

[SOLVED] Calculate relative time in C#

Sponsored Content