1

I am trying to format Date based on Locale value, I have gone through this post in SO but I am not able to get the data based on my requirement.

Here is what I have tried:

DateFormat f = DateFormat.getDateInstance(DateFormat.SHORT, Locale.UK);

String d = f.format(new Date());

System.out.println(d);

This prints the output as :27/03/15

But for the year I want it to be printed as 2015 instead of 15 as shown in the output.

In case of US locale the output should be 03/27/2015

Community
  • 1
  • 1
learner
  • 6,062
  • 14
  • 79
  • 139
  • mm is Minutes - try MM (for month) – griFlo Mar 27 '15 at 13:43
  • I think 'mm' is minute. Use 'MM' instead – heldt Mar 27 '15 at 13:43
  • Please note: 1) the `Date(String)` constructor is **deprecated** since like ... ever. You should **never ever** create a Date object this way. 2) If you are free in which Java version to use, you might completely forget about the old Date stuff; and immediately turn to the (somehow much better new Java8 date/time API); see https://docs.oracle.com/javase/tutorial/datetime/ – GhostCat Mar 27 '15 at 13:45
  • before SO, you should check out the docs first... http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html ... there are many other useful classes... check them out.. https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html – denvercoder9 Mar 27 '15 at 13:54
  • @RafiduzzamanSonnet, The docs do not have information on how can I implement the requirement which I have mentioned here. – learner Mar 27 '15 at 13:58
  • @EddyG My intention is how to get the required output, this is sample example I am trying. – learner Mar 27 '15 at 13:59

3 Answers3

0

MM instead of mm

SimpleDateFormat sf = new SimpleDateFormat("MM/dd/yyyy", Locale.UK);
injecteer
  • 20,038
  • 4
  • 45
  • 89
0

try this

SimpleDateFormat sf = new SimpleDateFormat("MM/dd/yyyy");

mm means minutes, MM - months, also Locale here has no effect

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • Sorry, I have updated my question as my earlier question was confusing. My requirement is to output the results based on locale. Now I have posted the output on how it should be for US and for UK. – learner Mar 27 '15 at 13:47
  • I need the output based on locale, can you please tell me how can we do that? – learner Mar 27 '15 at 13:48
0

Either you use the predefined formats which are crafted to be inline with the format for the given locale. Or you create a wrapper class/method to use the defaults from the JDK merged with your exceptional formats.

here an example for a wrapper method

public static void main(String[] args) throws Exception {
    DateFormat f = getDateFormat(Locale.UK);
    System.out.println("UK: " + f.format(new Date()));

    f = getDateFormat(Locale.US);
    System.out.println("US: " + f.format(new Date()));
}

static DateFormat getDateFormat(Locale locale) {
    if (locale == Locale.UK) {
        return new SimpleDateFormat("dd/MM/yyyy", Locale.UK);
    } else {
        return DateFormat.getDateInstance(DateFormat.SHORT, locale);
    }
}
SubOptimal
  • 22,518
  • 3
  • 53
  • 69