-3

I tried the below code but it gives me the name of the day of week two days ago.

  DatePicker picker;
  int date = picker.DayOfMonth;
  int month = (picker.Month + 1);//month is 0 based
  int year = picker.Year;

  SimpleDateFormat simpledateformat = new SimpleDateFormat("EEE");
  Date dt = new Date(year, month, date);
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Did you try this String dayOfTheWeek = (String) android.text.format.DateFormat.format("EEEE", date); you need to pass the date object and it will do the rest, Output: Thursday – Rakshit Nawani Apr 09 '18 at 04:27
  • 1
    Codewise there is nothing wrong with the use of SimpleFormatter. The thing is, if you are using the date constructor as shown in the example, both month and date parameters are 0 based – Jhilton Apr 09 '18 at 04:31
  • One thing to note though, in 'Date(year, month, date)' , month parameter is also 0-based but you're using 'int month = (picker.Month + 1);. You should try 'int month = picker.Month; – Karan Modi Apr 09 '18 at 04:32
  • 1
    After looking at the answers all I can say is that some developers are making some others lazy. Care to explain the answers guys? :( – Rohit5k2 Apr 09 '18 at 04:33
  • Week name? Do you mean day-of-week? – Basil Bourque Apr 09 '18 at 05:36
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Apr 09 '18 at 06:44
  • Your code, is it compilable Java code? The way I read [the DatePicker documentation](https://developer.android.com/reference/android/widget/DatePicker.html) it hasn’t got fields `DayOfMonth`, `Month` and `Year` (and the initial capital letters disagree with Java naming conventions). – Ole V.V. Apr 09 '18 at 06:51
  • Welcome to Stack Overflow. Please search and research before asking (see [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask)) For the benefit of yourself and everyone: you will get a good answer faster, and we will have fewer duplicate questions lying around. – Ole V.V. Apr 09 '18 at 07:04

4 Answers4

5

First convert your Date in to specific Date format using SimpleDateFormat

Use SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE"); to get Day name in week

WHERE EEEE -> Day name in week

SAMPLE CODE

 SimpleDateFormat inFormat = new SimpleDateFormat("dd-MM-yyyy");
 try {
       Date myDate = inFormat.parse(date+"-"+month+"-"+year);
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE");
       String dayName=simpleDateFormat.format(myDate);
 } catch (ParseException e) {
     e.printStackTrace();
}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • 1
    Thanks @Nilesh Rathod ,convert your Date in to specific Date format solved the problem. – Prachi Karapurkar Apr 09 '18 at 04:59
  • Hand formatting and them parsing back. That’s overcomplicating things. And you’re still using the long outdated and notoriously troublesome `SimpleDateFormat` class. – Ole V.V. Apr 09 '18 at 06:46
1
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE", Locale.US); 
String asWeek = dateFormat.format(dt);
Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126
0
    DateTimeFormatter dayOfWeekFormatter 
            = DateTimeFormatter.ofPattern("EEE", Locale.ENGLISH);
    LocalDate date = LocalDate.of(
            picker.getYear(), picker.getMonth(), picker.getDayOfMonth());
    System.out.println(date.format(dayOfWeekFormatter));

Picking 2018-04-09 this printed

Mon

I am using and recommending java.time, the modern Java date and time API. The Date class is long outdated, and you are using a deprecated constructor. It was deprecated because it works unreliably across time zones, so you shouldn’t. SimpleDateFormat is not only outdated, it is also notoriously troublesome. I recommend you avoid those classes altogether. The modern API is so much nicer to work with.

What went wrong in your code?

You’ve got two bugs apart from using the deprecated Date constructor and the outdated classes:

  1. It’s the Date’s month that is 0-based (not that of DatePicker), so you need to subtract 1, not add 1 (or maybe they are both 0-based??).
  2. The deprecated Date constructor’s year is “1900-based”. This may have seemed a good idea when the class was designed in the 1990’s: you could just specify 95 to get 1995. When you pass 2018 to the constructor, you get year 3918. That’s right. :-(

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, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new 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
  • correct me if i'm wrong **`DateTimeFormatter.ofPattern`** will required **`@RequiresApi(api = Build.VERSION_CODES.O)`** this is just for information – AskNilesh Apr 11 '18 at 12:29
  • I suspect you’ve been looking at `java.time.format.DateTimeFormatter`, @NileshRathod. I tried to explain that you should use `org.threeten.bp.format.DateTimeFormatter`. Try it, it shouldn’t require any high API level. `Build.VERSION_CODES.O` seems to agree with the level 26 I mentioned for the built-in classes like `java.time.format.DateTimeFormatter`. – Ole V.V. Apr 11 '18 at 12:40
0

Calendar calendar = Calendar.getInstance(); DateFormat date4= new SimpleDateFormat("EEEE", Locale.getDefault()); String localTime4 = date4.format(calendar.getTime());

Simple and easy way just use this

Noaman Akram
  • 3,680
  • 4
  • 20
  • 37