tas
tas

Reputation: 25

Java Date localization

How can I set the localization so that java.util.Date have the German format? ( ie: dd.mm.yyyy ) . I need to send to a Webservice a Date(), but it can be only on this format. Not as String but as a Date Object.

Thank you

Upvotes: 1

Views: 691

Answers (4)

Basil Bourque
Basil Bourque

Reputation: 338181

The modern solution uses java.time.

Avoid legacy date-time classes

You are using terribly flawed classes that were years ago supplanted by the modern java.time classes defined in JSR 310. You will find java.time built into Java 8+.

LocalDate

To represent a date-only value, without a time-of-day, and without a time zone or offset, use the java.time.LocalDate class.

LocalDate ld = LocalDate.of( 2024 , Month.JANUARY , 23 ) ;

the German format

To generate text in a localized format, use DateTimeFormatter.ofLocalized…. Specify a Locale to determine the localization rules to be using in translation and cultural norms.

Locale locale = Locale.GERMANY ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT ).withLocale( locale ) ;
String output = ld.format( f ) ;

If that localization is not to you taste, define a custom formatting pattern with DateTimeFormatter.ofPattern.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd.MM.uuuu" ) ;

need to send to a Webservice a Date(), but it can be only on this format.

The ideal solution would be to educate the developers of that Web service about using only standard ISO 8601 formats for exchanging date-time values rather than localized formats.

In the meantime, use DateTimeFormatter.ofLocalizedDate or DateTimeFormatter.ofPattern as discussed above.

Upvotes: 0

Ravinder Reddy
Ravinder Reddy

Reputation: 23982

As mentioned by Eric, java.util.Date does not have a format. If your Web Service returning a Date as a Date object, then the WS client should convert it to its desired format. In your case it is dd.mm.yyyy. Simple and best utility is SimpleDateFormat that formats a Date to the desired pattern and returns a String. You can't deny a String here because you need a formatted date object.

Upvotes: 0

COD3BOY
COD3BOY

Reputation: 12092

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner.

Something like this would serve your case,

Date today;
String output;
SimpleDateFormat formatter;

formatter = new SimpleDateFormat("dd.MM.yyyy");
today = new Date();
output = formatter.format(today);
System.out.println(output);

See this for more help : Customizing Formats

Upvotes: 1

Eric Rosenberg
Eric Rosenberg

Reputation: 1533

A java.util.Date doesn't have a format. The format only comes into play when you parse a String as a Date or format a Date for display. Internally the date is just a long.

To format an instance of a Date object as a String you can use the SimpleDateFormat class in java.text

formatter = new SimpleDateFormatter("dd.mm.yyyy");
String formattedDate = formatter.format(new Date());

Upvotes: 6

Related Questions