简体   繁体   中英

How to change the day of week to match an incremented date?

I have the following code to increment a calendar month by one month.

c.add(Calendar.MONTH, 1);
SimpleDateFormat sdf = new SimpleDateFormat("MMM-dd-yyyy");
String oneMonth = sdf.format(c.getTime());

When the new date is incremented, I display it in a textView. I would like to also display the day of the week that coorisponds to the new incremented date. How would I accomplish that with code? I tried setting this new code underneath the above code but it is not setting the day of the week correctly in this instance.

SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
Date d = new Date();
String dayOfTheWeek = sdf.format(d);

Can someone help me to accomplish this? I appreciate your time and assistance.

UPDATE::: SOLUTION that works for me:

adding "EEEE" to the original simpledate format like this...

            SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMM-dd-yyyy");
            String oneMonth = sdf.format(c.getTime());

TL;DR

    LocalDate date = LocalDate.of(2017, Month.DECEMBER, 13);

    date = date.plusMonths(1);
    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MMM-dd-yyyy", Locale.ENGLISH);
    String oneMonth = date.format(dateFormatter);
    System.out.println(oneMonth);
    DateTimeFormatter dayOfWeekFormatter = DateTimeFormatter.ofPattern("EEEE", Locale.ENGLISH);
    String dayOfTheWeek = date.format(dayOfWeekFormatter);
    System.out.println(dayOfTheWeek);

This prints

Jan-13-2018
Saturday

java.time AKA JSR 310

Even on Android, consider using JSR 310, the modern Java date and time API also known as java.time . It is so much nicer to work with. Can you do that on Android yet? Certainly! Get the ThreeTenABP and go ahead. Link to a question explaining in detail is at the end of this answer.

And for anyone reading along and not coding for Android: The modern API is built-in in Java 8 and later. On Java 6 and 7 you need the ThreeTen Backport, I provide a link at the end.

What went wrong in your code?

I don't know what you did wrong. As I said in comments, your code works for me. You said that the incremented date changed when the user input of another date. One possible explanation of such behaviour is if you are using one Calendar object (maybe referenced from two variables) for two purposes. While such a bug can of course be tracked down and fixed, you would be very unlikely to introduce it in the first place with the modern API because plusMonths() creates a new LocalDate object, leaving the original one unmodified (in fact it is unmodifiable).

Links

If you have a Date type, pass it into this method and it will return you the correct day. Note how it uses Calendar and Calendar.DAY_OF_WEEK to get what you are looking for.

public static String getDateDay(Date date){

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);

    int day = calendar.get(Calendar.DAY_OF_WEEK);

    String dayOfWeek = null;
    switch (day){
        case Calendar.MONDAY:
            dayOfWeek = "Monday";
            break;
        case Calendar.TUESDAY:
            dayOfWeek = "Tuesday";
            break;
        case Calendar.WEDNESDAY:
            dayOfWeek = "Wednesday";
            break;
        case Calendar.THURSDAY:
            dayOfWeek = "Thursday";
            break;
        case Calendar.FRIDAY:
            dayOfWeek = "Friday";
            break;
        case Calendar.SATURDAY:
            dayOfWeek = "Saturday";
            break;
        case Calendar.SUNDAY:
            dayOfWeek = "Sunday";
            break;
    }

    return dayOfWeek;
}

I figured out my problem and it was so simple that I don't believe I missed it. I just had to add "EEEE" to the original simple date format of "MM-dd-yyyy" like this "EEEE, MM-dd-yyyy" that outputs the correct day of week along with date. Sorry for the waste of time but I appreciate your time and the information. I am learning more every day. Thanks again to those who tried to help me. Much appreciated!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM