Java 日期格式逻辑
我得到了日期格式的代码。我的examDateFormat是这样打印出来的:Java 日期格式逻辑,java,datetime,calendar,date-format,android-calendar,Java,Datetime,Calendar,Date Format,Android Calendar,我得到了日期格式的代码。我的examDateFormat是这样打印出来的: Jan 01 = 1 Feb 02 = 11 March 03 = 21 April 04 = 31 for (ExamTimetable exam: listOfExam) { Calendar cal = Calendar.getInstance(); DateFormat format = new SimpleDateFormat("yyyy-MM-
Jan 01 = 1
Feb 02 = 11
March 03 = 21
April 04 = 31
for (ExamTimetable exam: listOfExam) {
Calendar cal = Calendar.getInstance();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
Date date = null;
date = format.parse(exam.getM_scheduledDate());
cal.setTime(date); //correct date
String examDateFormat = cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.MONTH) + 1 + "/" + cal.get(Calendar.YEAR);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
String day = new DateFormatSymbols().getShortWeekdays()[dayOfWeek];
if (examDateFormat.equals(mDates)) {
dataList.add(new ExamInfo(exam.getM_subjectFullName(), exam.getM_subjectCode(), examDateFormat + " " + "(" + day + ")", exam.getM_startTime(), exam.getM_endTime(), exam.getM_venueId(), exam.getM_seatingNo()));
}
}
例如,在二月,它应该显示02,但它显示11。我不知道如何修正这种逻辑。请帮我看看。谢谢
for (ExamTimetable exam: listOfExam) {
Calendar cal = Calendar.getInstance();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
Date date = null;
date = format.parse(exam.getM_scheduledDate());
cal.setTime(date); //correct date
String examDateFormat = cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.MONTH) + 1 + "/" + cal.get(Calendar.YEAR);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
String day = new DateFormatSymbols().getShortWeekdays()[dayOfWeek];
if (examDateFormat.equals(mDates)) {
dataList.add(new ExamInfo(exam.getM_subjectFullName(), exam.getM_subjectCode(), examDateFormat + " " + "(" + day + ")", exam.getM_startTime(), exam.getM_endTime(), exam.getM_venueId(), exam.getM_seatingNo()));
}
}
输入完整的代码,包括打印逻辑。这段代码很难推断出任何东西。请展示一个简短但完整的程序来演示这个问题。我们不知道代码中的值的一半是什么。看起来您只是在格式化日期,以便与另一个值进行比较——为什么不解析另一个值呢?如果必须对其进行格式化,为什么不使用
DateFormat
进行格式化?我还强烈建议使用Joda Time或java.Time,而不是java.util.Date
和java.util.Calendar
。你能给我们看一下“exam.getM_scheduledDate()”吗?