-1

I'm having issues with the below code displays Thursday as the dayOfTheWeek regardless of the date. Any ideas where I've gone wrong with this?

public void CreatePlan() {
    editPlanName = findViewById(R.id.editPlanName);
    String plan_name = editPlanName.getText().toString();
    DatabaseManager db;
    int day = datepicker.getDayOfMonth();
    int month = datepicker.getMonth();
    int year = datepicker.getYear();
    SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
    Integer d_name = day;
    Integer plan_recipe = 0;
    Log.d("Date", String.valueOf(d_name));
    String dayOfTheWeek = sdf.format(d_name);
    String date = day + "/" + month + "/" +year;
    db = new DatabaseManager(getApplicationContext());
    Log.d("Recipe name", recipe_name);
    db.createPlanRecipe(d_name, date, dayOfTheWeek, recipe_name);
    db.createPlan(plan_name, plan_recipe);
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [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. Oct 04 '20 at 09:28

3 Answers3

0

You have to create Date from your datepicker, then format it to find day like below:

  int day = datePicker.getDayOfMonth();
  int month = datePicker.getMonth();
  int year =  datePicker.getYear();

  Calendar calendar = Calendar.getInstance();
  calendar.set(year, month - 1, day);

  SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
  dayOfTheWeek = sdf.format(calendar.getTime());
Fahad Nasrullah
  • 329
  • 1
  • 5
0

… Any ideas where I've gone wrong with this?

day in your program is the day of the month from 1 to 31. Therefore d_name holds this number too.

Your SimpleDateFormat accepts formatting a number as a date and time expecting a count of milliseconds since the epoch of January 1, 1970 at 00:00 in UTC. So it will always format a date and time within the first 31 milliseconds after the epoch. Depending on your time zone the point in time that you format falls either on Wednesday, December 31, 1969 or on Thursday, January 1, 1970. So you will either always get Wednesday or always Thursday.

SimpleDateFormat.format(Object) accepts either a Date or a Number. Since Integer is a subclass of Number, it works as described.

The SimpleDateFormat class is notoriously troublesome, you have seen but a small corner of the problems that people often have with it. The Calendar class used in one other answer is poorly designed too. Both are long outdated. I suggest you look into java.time, the modern Java date and time API instead.

Further link: My answer to another question about getting the day of week from an Android date picker.

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

You are getting the value of the day-of-the-month, the month and the year in the following lines of code but you are not setting these values into the Calendar object which is supposed to give you other information (e.g. the day-of-week) by processing these values:

int day = datepicker.getDayOfMonth();
int month = datepicker.getMonth();
int year = datepicker.getYear();

So, before you try to get any other information from the Calendar object, set these values to the object as shown below:

// Set the picked values into an instance of Calendar
Calendar calendar = Calendar.getInstance();
calendar.clear();// Make sure to call this to reset all fields
calendar.set(year, month - 1, day);// Make sure to decrease month by 1

Now, your rest of code will work as you are expecting e.g. let's say you select 4 as the day-of-the-month, 10 as the month, and 2020 as the year, the following code will give you Sunday as the day-of-the-week.

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Main {
    public static void main(String[] args) {
        int day = 4;
        int month = 10;
        int year = 2020;

        // Set the picked values into an instance of Calendar
        Calendar calendar = Calendar.getInstance();
        calendar.clear();// Make sure to call this to reset all fields
        calendar.set(year, month - 1, day);// Make sure to decrease month by 1
        System.out.println(calendar.getTime());

        // Your desired format
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");

        // The day-of-the-week for the specified date
        String dayOfTheWeek = sdf.format(calendar.getTime());
        System.out.println(dayOfTheWeek);
    }
}

Output:

Sun Oct 04 00:00:00 BST 2020
Sunday

Note that I have decreased the month (picked from the date-picker) by 1 because java.util date-time API is based on 0 as the month of January.

A piece of advice:

I recommend you switch from the outdated and error-prone java.util date-time API and SimpleDateFormat to the modern java.time date-time API and the corresponding formatting API (from the package, java.time.format). Learn more about the modern date-time API from Trail: Date Time.

If 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.

By using the modern date-time API:

import java.time.LocalDate;
import java.time.format.TextStyle;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        int dayOfMonth = 4;
        int month = 10;
        int year = 2020;

        // Instantiate a LocalDate object using the picked values
        LocalDate date = LocalDate.of(year, month, dayOfMonth);

        // The day-of-the-week for the specified date
        String dayOfTheWeek = date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH);
        System.out.println(dayOfTheWeek);
    }
}

Output:

Sunday
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110