英文:
SimpleDateFormat returning incorrect day on given date
问题
以下是翻译好的内容:
最近我在HackerRank上做了一个问题,被要求找出给定日期的星期几。我使用了SimpleDateFormat
来找到星期几。
下面是我的代码:
String sd = Integer.toString(day) + "-" + Integer.toString(month) + "-" + Integer.toString(year);
try {
Date d = new SimpleDateFormat("dd-MM-yyyy").parse(sd);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
return sdf.format(d).toUpperCase();
} catch (Exception e) {
return "";
}
有趣的是,上面的代码对于今天的日期即2020年3月4日(星期五)打印出了正确的结果,但对于2015年8月5日则返回了错误的星期几(应该返回星期三,但实际返回星期一)。
请帮助我找出问题所在。
谢谢。
编辑
我犯了一个小错误,我想使用每月的日期,应该使用dd
。DD
代表年份中的天数。这解决了我的问题!
英文:
I was recently doing a question on HackerRank in which I was asked to find the day on a given date. I used SimpleDateFormat
to find the day.
Below is my code:
String sd = Integer.toString(day) + "-" + Integer.toString(month) + "-" + Integer.toString(year);
try {
Date d = new SimpleDateFormat("DD-MM-yyyy").parse(sd);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
return sdf.format(d).toUpperCase();
} catch (Exception e) {
return "";
}
The funny thing here is that the above code is printing correct result for today's date i.e. 03/04/2020(Friday
) but it is returning incorrect day on 05/08/2015(it should return Wednesday
but instead it returns Monday
).
Please help me to find the problem.
Thank you.
EDIT
I was doing a little mistake that I wanted to use the day of the month, for which dd
has to be used. DD
represents the day of the year. That solved my problem!
答案1
得分: 2
你应该使用 SimpleDateFormat("dd-MM-yyyy")
。
DD 代表年中的日期,就像一年有 365 天。
英文:
You should be using SimpleDateFormat("dd-MM-yyyy")
.
DD is for day in year, like there are 365 days in a year.
答案2
得分: 1
问题的原因是使用了 DD
而不是 dd
。
我还建议您不要使用过时的日期/时间 API,例如 java.util.Date 和 SimpleDateFormat。相反,您应该使用现代的日期/时间 API,例如来自包 java.time 和 java.time.format 的类型,如下所示:
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.TextStyle;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
int day = 5;
int month = 8;
int year = 2010;
LocalDate date = LocalDate.of(year, month, day);
DayOfWeek dayOfWeek = date.getDayOfWeek();
System.out.println(dayOfWeek);
System.out.println(dayOfWeek.getDisplayName(TextStyle.FULL, Locale.US));
System.out.println(dayOfWeek.getDisplayName(TextStyle.SHORT, Locale.US));
}
}
输出:
THURSDAY
Thursday
Thu
使用 DateTimeFormatter
:
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
int day = 5;
int month = 8;
int year = 2010;
String sd = String.format("%02d", day) + "-" + String.format("%02d", month) + "-" + String.format("%04d", year);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate date = LocalDate.parse(sd, formatter);
DayOfWeek dayOfWeek = date.getDayOfWeek();
System.out.println(dayOfWeek);
System.out.println(dayOfWeek.getDisplayName(TextStyle.FULL, Locale.US));
System.out.println(dayOfWeek.getDisplayName(TextStyle.SHORT, Locale.US));
}
}
输出:
THURSDAY
Thursday
Thu
英文:
The cause of the problem is using DD
instead of dd
.
I also suggest you not to use the outdated date/time API e.g. java.util.Date and SimpleDateFormat. Instead, you should use the modern date/time API e.g. types from the packages, java.time and java.time.format as shown below:
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.TextStyle;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
int day = 5;
int month = 8;
int year = 2010;
LocalDate date = LocalDate.of(year, month, day);
DayOfWeek dayOfWeek = date.getDayOfWeek();
System.out.println(dayOfWeek);
System.out.println(dayOfWeek.getDisplayName(TextStyle.FULL, Locale.US));
System.out.println(dayOfWeek.getDisplayName(TextStyle.SHORT, Locale.US));
}
}
Output:
THURSDAY
Thursday
Thu
Using DateTimeFormatter
:
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
int day = 5;
int month = 8;
int year = 2010;
String sd = String.format("%02d", day) + "-" + String.format("%02d", month) + "-" + String.format("%04d", year);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate date = LocalDate.parse(sd, formatter);
DayOfWeek dayOfWeek = date.getDayOfWeek();
System.out.println(dayOfWeek);
System.out.println(dayOfWeek.getDisplayName(TextStyle.FULL, Locale.US));
System.out.println(dayOfWeek.getDisplayName(TextStyle.SHORT, Locale.US));
}
}
Output:
THURSDAY
Thursday
Thu
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论