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.
当新日期递增时,我将其显示在textView中。我还想显示与新增加日期协调的星期几。我如何用代码实现这一目标?我尝试在上面的代码下面设置这个新代码,但是在这个实例中没有正确设置星期几。
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...
将“EEEE”添加到原始的简单格式,如下所示......
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMM-dd-yyyy");
String oneMonth = sdf.format(c.getTime());
3 个解决方案
#1
0
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.
即使在Android上,也可以考虑使用JSR 310,即现代Java日期和时间API,也称为java.time。与它合作真是太好了。你能在Android上做到吗?当然!获得ThreeTenABP并继续。链接到详细解释的问题在本答案的最后。
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.
对于任何阅读并且不编码Android的人来说:现代API内置于Java 8及更高版本。在Java 6和7上你需要ThreeTen Backport,我在最后提供了一个链接。
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).
我不知道你做错了什么。正如我在评论中所说,您的代码适合我。您说当用户输入另一个日期时,递增的日期会更改。这种行为的一种可能解释是,如果您使用一个Calendar对象(可能从两个变量引用)有两个目的。虽然这样的bug当然可以被追踪和修复,但是你很可能不会首先使用现代API来引入它,因为plusMonths()会创建一个新的LocalDate对象,而原始的就不会被修改(实际上它是不可修改的) )。
Links
- Oracle tutorial trail: Date Time
- Question: How to use ThreeTenABP in Android Project
- ThreeTen Backport home page
- Java Specification Request (JSR) 310
Oracle教程:日期时间
问题:如何在Android项目中使用ThreeTenABP
ThreeTen Backport主页
Java规范请求(JSR)310
#2
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.
如果您有日期类型,请将其传递给此方法,它将返回正确的日期。请注意它如何使用Calendar和Calendar.DAY_OF_WEEK来获取您要查找的内容。
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;
}
#3
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!
我想出了我的问题,这很简单,我不相信我错过了它。我只需要将“EEEE”添加到“MM-dd-yyyy”的原始简单日期格式,就像这个“EEEE,MM-dd-yyyy”一样,输出正确的星期几和日期。抱歉浪费时间,但感谢您的时间和信息。我每天都在学习更多。再次感谢那些试图帮助我的人。非常感激!
#1
0
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.
即使在Android上,也可以考虑使用JSR 310,即现代Java日期和时间API,也称为java.time。与它合作真是太好了。你能在Android上做到吗?当然!获得ThreeTenABP并继续。链接到详细解释的问题在本答案的最后。
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.
对于任何阅读并且不编码Android的人来说:现代API内置于Java 8及更高版本。在Java 6和7上你需要ThreeTen Backport,我在最后提供了一个链接。
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).
我不知道你做错了什么。正如我在评论中所说,您的代码适合我。您说当用户输入另一个日期时,递增的日期会更改。这种行为的一种可能解释是,如果您使用一个Calendar对象(可能从两个变量引用)有两个目的。虽然这样的bug当然可以被追踪和修复,但是你很可能不会首先使用现代API来引入它,因为plusMonths()会创建一个新的LocalDate对象,而原始的就不会被修改(实际上它是不可修改的) )。
Links
- Oracle tutorial trail: Date Time
- Question: How to use ThreeTenABP in Android Project
- ThreeTen Backport home page
- Java Specification Request (JSR) 310
Oracle教程:日期时间
问题:如何在Android项目中使用ThreeTenABP
ThreeTen Backport主页
Java规范请求(JSR)310
#2
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.
如果您有日期类型,请将其传递给此方法,它将返回正确的日期。请注意它如何使用Calendar和Calendar.DAY_OF_WEEK来获取您要查找的内容。
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;
}
#3
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!
我想出了我的问题,这很简单,我不相信我错过了它。我只需要将“EEEE”添加到“MM-dd-yyyy”的原始简单日期格式,就像这个“EEEE,MM-dd-yyyy”一样,输出正确的星期几和日期。抱歉浪费时间,但感谢您的时间和信息。我每天都在学习更多。再次感谢那些试图帮助我的人。非常感激!