Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the date format for a locale

I've been using DateFormat to format dates, but now I need to pass the format for the locale to the jQuery Date widget. How can I get the format?

Update:

To clarify, I need to get the pattern in Java so I can output it elsewhere. e.g. in some JavaScript where I might want to create a string like this with the date format in it:

$('#datepicker').datepicker({'dateFormat':'dd/mm/yy'})
like image 993
Sam Hasler Avatar asked Feb 18 '11 14:02

Sam Hasler


People also ask

What is locale date string?

The toLocaleDateString() method returns a string with a language-sensitive representation of the date portion of the specified date in the user agent's timezone. In implementations with Intl. DateTimeFormat API support, this method simply calls Intl. DateTimeFormat .

How do I show the date format?

The dates appear as, mm/dd/yyyy in the U.S. and as, dd/mm/yyyy outside the U.S. where mm is the month, dd is the day, and yyyy is the year. The time is displayed as, hh:mm:ss AM/PM, where hh is the hour, mm is minutes, and ss is seconds.


1 Answers

You can get the date pattern by using

((SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.UK)).toPattern()

or if you just need the date

((SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, Locale.UK)).toPattern()

API: SimpleDateFormat.toPattern(), DateFormat.getDateTimeInstance(), DateFormat.getDateInstance()

Second question is how to convert that to jQuery specific format pattern.

Java date formatting. vs jQuery DatePicker Date Formatting

  • day of year in java D, in jQuery o
  • short year in Java yy, in jQuery y
  • long year in Java yyyy, in jQuery yy
  • Month without leading zeros in Java M, in jQuery m
  • Month with leading zeros in Java MM, in jQuery mm
  • Month name short in Java MMM, in jQuery M
  • Month name long in Java MMMM in jQuery MM -
like image 71
Aleksi Yrttiaho Avatar answered Oct 03 '22 01:10

Aleksi Yrttiaho