0

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());
Stark
  • 107
  • 1
  • 11
  • What result do you get instead of the correct result? Your code looks correct in my eyes. – Ole V.V. Dec 13 '17 at 10:22
  • Your code works for me. Two potential issues: (1) Did you remember to pass the date of the `Calendar` into the latter formatter in the same way as with the former? (2) You should pass a `Locale` to the formatter so it knows which language you want. – Ole V.V. Dec 13 '17 at 10:42
  • Even on Android, consider using JSR 310, the modern Java date and time API also known as `java.time`, through the [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP), the Android backport of the API. It is so much nicer to work with. `yourLocalDate.plusMonths(1).format(DateTimeFormatter.ofPattern("EEEE", Locale.ENGLISH))`. If `yourLocalDate` is Dec 13, 2017, this yields `Saturday` because Jan 13 , 2018 is a Saturday. – Ole V.V. Dec 13 '17 at 11:12
  • Ole, thank you. It does work with one exception that I think I forgot to explain. The new incremented date will change with the user input of another date. When I use my solution above, it does change the day of the week but it matches the new user inputted date and not the new incremented date. I need it to match the new incremented date based on how that date changed because of the new user input date. Any ideas? – Stark Dec 13 '17 at 11:27
  • Ole, I will try the local date format you mentioned above and let you know if it works for me. Thanks again. – Stark Dec 13 '17 at 12:10
  • it may sound like you are using one `Calendar` object (maybe referenced from two variables) for two purposes. I and we can help you better if you can [create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) demonstrating the unwanted behaviour. – Ole V.V. Dec 13 '17 at 12:14

3 Answers3

1

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Thanks again Ole. I am looking into this. Also want to be clear about my issue. I have a textfield that populates with the current date including the day of the week. If the user doesn't want the current date, they touch the text field and they can choose a new date from the date picker. When they do, it repopulates that textfield with the new selected day and the new corresponding day of the week. Value of that field automatically alters another field that has additional dates that increment out by one, three, or six months. Dates themselves change but the day of week in other field does not. – Stark Dec 14 '17 at 00:38
  • The day of the week in the other field does not change. It just automatically inputs the same day of week from the other textfield but I need it to change, based on the user change, to correspond with the dates in the additional textfield that automatically change. I hope that sort of makes sense. – Stark Dec 14 '17 at 00:40
  • 1
    Ole, 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! – Stark Dec 14 '17 at 01:00
0

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;
}
sark9012
  • 5,485
  • 18
  • 61
  • 99
  • Thank you, I will try this. – Stark Dec 13 '17 at 01:34
  • Sorry, I discourage using this answer. It’s both safer and easier to use a standard date formatter as in the question, whether you want the modern `DateTimeFormatter` or the old `SimpleDateFormat` that is already built-in on Android devices. – Ole V.V. Dec 13 '17 at 10:39
0

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!

Stark
  • 107
  • 1
  • 11