I understand that if I want to convert a text into date with locale setting, I can use SimpleDateFormat constructor passing the pattern and locale. Then parse the text and format the date returned from parsing. What I am trying to do is return a date instance instead of string returned from SimpleDateFormat.format(date). Parsing it again will return date in english but I need a date instance in the locale like locale.russia. So basically if I input a string “16 nov 2016” I need Russian version of it as an instance of date not as a string.
Asked
Active
Viewed 247 times
-3
-
1A `Date` doesn’t have a locale setting nor a format in it. It’s only a point in time. If this was what you were after. – Ole V.V. Oct 23 '17 at 07:37
-
1Any reason why you are sticking with the long outdated classes `Date` and `SimpleDateFormat`? I recommend migrating to [the modern Java date and time API known as JSR-310 or `java.time`](https://docs.oracle.com/javase/tutorial/datetime/), the sooner the better. – Ole V.V. Oct 23 '17 at 07:41
-
@OleV.V. I will look into it. It is an existing code that I m working on. – Pete sam Oct 23 '17 at 11:42
-
1@Petesam Do all your new code using java.time. At the boundaries of your old code, convert to/from java.time using new methods added to the old classes such as [`java.util.Date::toInstant`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html#toInstant--) and [`java.util.Date.from( Instant )`](https://docs.oracle.com/javase/9/docs/api/java/util/Date.html#from-java.time.Instant-). – Basil Bourque Oct 23 '17 at 14:46
1 Answers
-1
Java 8 to rescue :
String date = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
.withLocale(new Locale("ru"))
.format(LocalDate.of(YYYY, MM, DD));
System.out.println(date);

Code_Eat_Sleep
- 303
- 2
- 6
-
1It’s as close as we can get. The asker did ask for a `Date` instance, not a string, in Russian, but since a `Date` doesn’t hold a language or locale in it, this is not possible. – Ole V.V. Oct 23 '17 at 07:40