6
public class DefaultDateFormatPattern {
    public static void main(String[] args) {
        System.out.println(new Date());
    }
}

The output is: Thu Jan 08 10:52:56 IST 2015

Is there any method in Java to get the pattern of the date it is showing ?

wassgren
  • 18,651
  • 6
  • 63
  • 77
Alin
  • 314
  • 1
  • 3
  • 9
  • By "pattern", do you mean a pattern string that can be used with `java.text.SimpleDateFormat`? – ez121sl Jan 08 '15 at 05:34
  • If you mean the current default platform date, then you should use `Locales` and `getDefault()` – Sharp Edge Jan 08 '15 at 05:38
  • I recommend you don’t use `Date` and `SimpleDateFormat`. Those classes are poorly designed and long outdated. Instead use `Instant` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) for a moment in time. The format produced by its `toString` method is in `DateTimeFormatter.ISO_INSTANT`. – Ole V.V. May 22 '19 at 12:31

4 Answers4

7

According to Java 8 toString() method of Date class the format is: EEE MMM dd HH:mm:ss zzz yyyy.

vezunchik
  • 3,669
  • 3
  • 16
  • 25
muaz
  • 550
  • 9
  • 15
4

You can try SimpleDateFormat.toLocalizedPattern() or SimpleDateFormat.toPattern() to get the pattern.

SimpleDateFormat format=new SimpleDateFormat();
System.out.println(format.toLocalizedPattern());
System.out.println(format.toPattern());

Pass locale to get the locale specific pattern.

Read similar post

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • 1
    The SimpleDateFormat constructor needs the pattern with locale. But I want to know pattern from the date it is printing for my locale. – Alin Jan 08 '15 at 05:46
  • Look at `Date.toString()` method in source code and look what it does. – Braj Jan 08 '15 at 05:49
0
 Calendar rightNow = Calendar.getInstance();

 DateFormat d = new SimpleDateFormat("DDD", Locale.getDefault());

 String day = d.format(rightNow.getTime());

You can use the overloaded constructor of SimpleDateFormat to get the current default use Locale.getDefault() in the 2nd argument.. Check the SimpleDateFormat Api Docs

Sharp Edge
  • 4,144
  • 2
  • 27
  • 41
0

You can use SimpleDateFormat to get whatever the date format you want. The default format of SimpleDateFormat is

d/M/yy h:mm a

you can set the way you want.

Eg.

SimpleDateFormat format=new SimpleDateFormat("dd/MM/yyyy");
System.out.println(format.format(new Date()));
Pavan
  • 337
  • 1
  • 4
  • 10