2

I want my java program to take the system date format .

Ex: when i change the date format in the system from mm-dd-yy to yyyy-MM-dd. I want my java program to pull the short date format from the system and display in my program.

Again if I change the system format to M/D/YY, my java program to display this.

I tried few options like ,

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat();
System.out.println(System.currentTimeMillis());
System.out.println(cal.getTime());
System.out.println( sdf.format(cal.getTime()) );

this always prints mm/dd/yy inspite of having yyyy-mm-dd format in the system.

Any help would be appreciated.

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
user3754993
  • 143
  • 1
  • 2
  • 8
  • I'm not certain it's possible, Java uses the locale information to make decisions about which format it should use for `DateFormat`, it doesn't use any of the system configuration, which would be nice – MadProgrammer Jul 29 '15 at 06:03

5 Answers5

3

Use SimpleDateFormat.toLocalizedPattern()

From the Java Docs:

Returns a localized pattern string describing this date format.

Zaid Malhis
  • 588
  • 4
  • 18
  • This always returns, M/d/yy h:mm a date = 7/29/15 12:01 PM But my system format is yyyy-MM-dd – user3754993 Jul 29 '15 at 06:37
  • you are referring to the Operating System format? [this](http://stackoverflow.com/questions/6711925/how-can-i-so-date-and-time-formatting-in-java-that-respects-the-users-os-settin) question might be of help in that. – Zaid Malhis Jul 29 '15 at 06:47
  • I tried possible options provided in that link, but its not working as expected. – user3754993 Jul 29 '15 at 09:37
  • The link doesnt specify the exact solution for the problem. – user3754993 Jul 29 '15 at 09:38
0
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyy");
Date date = new Date();

and then simply print

dateFormat.format(date);
Mado Baker
  • 109
  • 1
  • 2
  • 11
  • 3
    This has nothing to do with the question asked. The question was how to use the configured system date format, not how to a DateFormat. – Florian Schaetz Jul 29 '15 at 06:40
0
public static String getDateFromLongformat(long dateInLong) {

    Date date = new Date(dateInLong);
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    String dateText = df.format(date);

    return dateText;
}
Ajmal Muhammad
  • 685
  • 7
  • 25
Mohd Mufiz
  • 2,236
  • 17
  • 28
0

We were using following approach for this

  1. Identify the timezone of the system
  2. have a dateformat mapping to preferred timezone
  3. use the dateformat from the mapping to display the date.

Step 2 was basically a properties file, where we mapped the timezone and preferred date format.

Mohit Kanwar
  • 2,962
  • 7
  • 39
  • 59
0

You can't retrieve the date format used in your operating system using Java but you can format the date based on the locale using SimpleDateFormat class.

Refer Doc: http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html

Loganathan Mohanraj
  • 1,736
  • 1
  • 13
  • 22