Fai*_*fan 2 java calendar date date-formatting
我有一个类DayOfWeek,它从用户那里获取日期并打印该日期的星期几.
例如,用户将输入他的日期:(使用格式MM DD YYYY)
07 22 2016
Run Code Online (Sandbox Code Playgroud)
输出应该是
星期五
这是我的班级:
public class DayOfWeek{
public static void main(String[] args){
System.out.println("Enter the date (Format: MM DD YYYY) :");
Scanner sc = new Scanner(System.in);
String month = sc.next();
String day = sc.next();
String year = sc.next();
int m = Integer.parseInt(month);
int d = Integer.parseInt(day);
int y = Integer.parseInt(year);
Calendar cal = Calendar.getInstance();
Date date = new Date(y, m, d);
cal.setTime(date);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
System.out.println((sdf.format(dayOfWeek)).toUpperCase());
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我输入上述日期(07 22 2016)时,我没有得到所需的输出(FRIDAY).相反,输出是我系统上当前的一周.我完全坚持问题所在.
你正在传递int给SimpleDateFormat...你应该传递Date价值.你根本不需要一个Calendar:
// BAD CODE: DON'T USE - KEEP READING
import java.text.*;
import java.util.*;
public class DayOfWeek {
public static void main(String[] args){
System.out.println("Enter the date (Format: MM DD YYYY) :");
Scanner sc = new Scanner(System.in);
String month = sc.next();
String day = sc.next();
String year = sc.next();
int m = Integer.parseInt(month);
int d = Integer.parseInt(day);
int y = Integer.parseInt(year);
// Note the changes here
Date date = new Date(y - 1900, m - 1, d);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
System.out.println(sdf.format(date).toUpperCase());
}
}
Run Code Online (Sandbox Code Playgroud)
我已经在那里发出了警告,因为您使用的是不推荐使用的Date构造函数 - 并且它已被弃用,原因很多.基本上,这java.util.Date是一个糟糕的 API,你应该尽可能避免使用它.您的原始呼叫new Date(y, m, d)被打破,因为第一个参数java.util.Date(int, int, int)表示具有1900个基数的年份(因此值为117表示2017年),第二个参数表示具有0个基数的月份(因此值6表示7月).然后是隐式时区转换,你真的不想要.
你会多过使用更好的java.time,而在这种情况下LocalDate类:
import java.time.*;
import java.time.format.*;
import java.util.*;
public class DayOfWeek {
public static void main(String[] args){
System.out.println("Enter the date (Format: MM DD YYYY) :");
Scanner sc = new Scanner(System.in);
String month = sc.next();
String day = sc.next();
String year = sc.next();
int m = Integer.parseInt(month);
int d = Integer.parseInt(day);
int y = Integer.parseInt(year);
LocalDate date = LocalDate.of(y, m, d);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE");
System.out.println(formatter.format(date).toUpperCase());
}
}
Run Code Online (Sandbox Code Playgroud)
最后,你解析用户输入的方法也不理想 - 我建议也使用a DateTimeFormatter:
import java.time.*;
import java.time.format.*;
public class DayOfWeek {
public static void main(String[] args){
System.out.println("Enter the date (Format: MM DD YYYY) :");
DateTimeFormatter parser = DateTimeFormatter.ofPattern("MM dd yyyy");
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
LocalDate date = LocalDate.parse(line, parser);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE");
System.out.println(formatter.format(date).toUpperCase());
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1407 次 |
| 最近记录: |