0

How to get name day name like (Wednesday - Thursday) from this date format "Wed Jan 30 00:00:00 GMT+02:00 2019"

Hossam Hassan
  • 795
  • 2
  • 13
  • 39
  • What you have got looks like an old-fashioned `Date` object. Is this what you have got? Or a string with the format you mention? – Ole V.V. Jan 30 '19 at 10:50
  • Possible duplicate of [Is there a date format to display the day of the week in java?](https://stackoverflow.com/questions/5121976/is-there-a-date-format-to-display-the-day-of-the-week-in-java) – Ole V.V. Jan 30 '19 at 10:51

4 Answers4

1

java.time

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.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in. Only in this case use yourOldfashionedDate.toInstant() instead of DateTimeUtils.toInstant(yourOldfashionedDate).
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

You can use SimpleDateFormat for it and the format part that gives you the full day name is EEEE. Hope it helps!

  • 1
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Jan 30 '19 at 10:48
0

You will need to first parse your String into a Calendar object using a SimpleDateFormat :

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
try {
    cal.setTime(sdf.parse("Wed Jan 30 00:00:00 GMT+02:00 2019"));
} catch (ParseException e) {
    // Log invalid date format
}

Then extract the day from your Calendar object:

String day = cal.getDisplayName(Calendar.DAY_OF_WEEK,Calendar.LONG,Locale.ENGLISH);
113408
  • 3,364
  • 6
  • 27
  • 54
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Jan 30 '19 at 10:48
0

## Here, I have attached a method that will give you expected output ##

public void dayName(){
    String weekDay,time;
    SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE MMM dd HH:mm", Locale.US);
    SimpleDateFormat dayFormat1 = new SimpleDateFormat("hh:mm yyyy", Locale.US);
    dayFormat.setTimeZone(TimeZone.getTimeZone("ITC"));
    dayFormat1.setTimeZone(TimeZone.getTimeZone("GMT"));
        Calendar calendar = Calendar.getInstance();
        weekDay = dayFormat.format(calendar.getTime());
        time=dayFormat1.format(calendar.getTime());
        Log.e(TAG, "dayName: "+weekDay+" GMT+"+time );// out put looks like : Tuesday Jan 29 17:58 GMT+05:58 2019

}
  • I have used two TimeZone are ITC and GMT
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Jan 30 '19 at 10:48