Reputation: 9011
What I want to achieve is to convert a date in format yyyyMMdd
to locale format i.e yyyy/MM/dd
or dd/MM/yyyy
etc.
I am not interested in the time part, I just require date.
The function would take string date and return a string date in locale format.
What I currently have is:
dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
convertedDate = dateFormat.parse("20120521");
Everything that I have tried after that either return me a long string with time and GMT etc, or the same string that I passed to the function.
Upvotes: 0
Views: 2179
Reputation: 78975
The java.util
Date-Time API and their formatting API, SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
Solution using java.time
, the modern Date-Time API:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// A sample date string
String strDate = "20210630";
DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("uuuuMMdd", Locale.ENGLISH);
LocalDate date = LocalDate.parse(strDate, dtfInput);
System.out.println(date.format(getShortDateFormatterForLocale(Locale.getDefault())));
System.out.println(date.format(getShortDateFormatterForLocale(Locale.GERMANY)));
System.out.println(date.format(getShortDateFormatterForLocale(Locale.US)));
}
static DateTimeFormatter getShortDateFormatterForLocale(Locale locale) {
return DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).localizedBy(locale);
}
}
Output on my system in the UK:
30/06/2021
30.06.21
6/30/21
Learn more about the modern Date-Time API from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
Upvotes: 1
Reputation: 2071
Here is an answer to your question: How can I format date by locale in Java?
an example:
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, yourLocale);
String formattedDate = df.format(yourDate);
Also if you have to do a lot with Dates, consider using Joda: Java, getting Date without time
Upvotes: 1
Reputation: 44813
You want to use the DateFormat built in types
DateFormat df = DateFormat.getDateInstance(DateFormat.Short, Locale.getDefault()));
As per the Javadocs
Use getDateInstance to get the normal date format for that country. There are other static factory methods available. Use getTimeInstance to get the time format for that country. Use getDateTimeInstance to get a date and time format. You can pass in different options to these factory methods to control the length of the result; from SHORT to MEDIUM to LONG to FULL. The exact result depends on the locale, but generally:
SHORT is completely numeric, such as 12.13.52 or 3:30pm MEDIUM is longer, such as Jan 12, 1952 LONG is longer, such as January 12, 1952 or 3:30:32pm FULL is pretty completely specified, such as Tuesday, April 12, 1952 AD or 3:30:42pm PST.
Upvotes: 1
Reputation: 1499800
It sounds like you've already got the parsing part sorted - that's entirely separate from the formatting part.
For formatting, I suspect you want:
DateFormat localeFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
String text = localeFormat.format(convertedDate);
... experiment with SHORT
, MEDIUM
, LONG
and FULL
to see which one meets your needs best, but I suspect it'll be SHORT
or MEDIUM
.
(You can omit the second argument to getDateInstance
and it will use the default locale, but personally I'd advise including it explicitly for clarity.)
Upvotes: 5