How to get name day name like (Wednesday - Thursday) from this date format "Wed Jan 30 00:00:00 GMT+02:00 2019"
It seems that what you’ve got is an instance of the java.util.Date
class. That’s a poorly designed class that is long outdated, so first thing is to see if you can avoid that and have an instance of a class from java.time, the modern Java date and time API, instead.
However, if you got the Date
from a legacy API that you cannot change or don’t want to change just now, first thing is to convert it to a modern Instant
and then perform further conversions from there. The following snippet uses ThreeTenABP, more on that below.
Date yourOldfashionedDate = getFromLegacyApi();
Instant modernInstant = DateTimeUtils.toInstant(yourOldfashionedDate);
String dayName = modernInstant.atZone(ZoneId.systemDefault())
.getDayOfWeek()
.getDisplayName(TextStyle.FULL, Locale.ENGLISH);
System.out.println("Day name is " + dayName);
Output given the date from your question:
Day name is Wednesday
If what you got was a String
(probably a string returned from Date.toString
at some point), you need to parse it first:
DateTimeFormatter dateFormatter
= DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ROOT);
String dateString = "Wed Jan 30 00:00:00 GMT+02:00 2019";
ZonedDateTime dateTime = ZonedDateTime.parse(dateString, dateFormatter);
String dayName = dateTime.getDayOfWeek()
.getDisplayName(TextStyle.FULL, Locale.ENGLISH);
You see that the last bit is exactly like before.
Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.
yourOldfashionedDate.toInstant()
instead of DateTimeUtils.toInstant(yourOldfashionedDate)
.org.threeten.bp
with subpackages.java.time
.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).