Java 将日期(MM/dd/yyyy)输入字符串
你好,我是java新手 我只是需要一些建议 这就是我需要做的: 用户输入日期为2018年8月15日,控制台应该有输出:“您好!这是我们的主2018年8月15日。” 这是我的密码Java 将日期(MM/dd/yyyy)输入字符串,java,Java,你好,我是java新手 我只是需要一些建议 这就是我需要做的: 用户输入日期为2018年8月15日,控制台应该有输出:“您好!这是我们的主2018年8月15日。” 这是我的密码 package newdate; import java.util.Scanner; public class NewDate { public static void main(String[] args) { Scanner sc = new Scanner(System.in);
package newdate;
import java.util.Scanner;
public class NewDate {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the date in mm/dd/yyyy: ");
String str=sc.next();
switch (str) {
case 1: str.substring(0,2) = "January";
break;
case 2: str.substring(0,2) = "February";
break;
case 3: str.substring(0,2) = "March";
break;
case 4: str.substring(0,2) = "April";
break;
case 5: str.substring(0,2) = "May";
break;
case 6: str.substring(0,2) = "June";
break;
case 7: str.substring(0,2) = "July";
break;
case 8: str.substring(0,2) = "August";
break;
case 9: str.substring(0,2) = "September";
break;
case 10: str.substring(0,2) = "October";
break;
case 11: str.substring(0,2) = "November";
break;
case 12: str.substring(0,2) = "December";
break;
default: str.substring(0,2) = "Invalid month";
break;
}
System.out.println("Hello! This is day " + str.substring(3,5) +" of the month of " + str.substring(0,2) +" in the year of our Lord" + str.substring(6,10));
}
}
问题是数据类型,我希望是数字的月份将替换为字符串(月份)
例:11月=11月
我该怎么做呢?首先,对于真实世界的编程,可以使用标准库中的
LocalDate
保存日期,并使用DateTimeFormatter
解析用户在LocalDate
中输入的日期。请参阅底部的链接
最基本的是,对于代码,您需要一个单独的字符串
变量作为月份名称。不能将其重新分配到已有的字符串中。只需声明字符串monthName编码>并在开关中为其指定适当的值
另外,当str
是字符串且1是int
时,您不能打开str
并将1用作大小写标签。简单的解决方法是打开str.substring(0,2)
(以字符串形式表示的月数),并使用“01”
、“02”
等作为案例标签。另一个常见的解决方案是将月号解析为int
(请参阅下面的另一个链接或搜索如何执行),然后保持案例标签1、2等不变
链接
- 解释如何使用
java.time
首先,对于实际编程,可以使用标准库中的LocalDate
保存日期,使用DateTimeFormatter
解析用户在LocalDate
中输入的日期。请参阅底部的链接
最基本的是,对于代码,您需要一个单独的字符串
变量作为月份名称。不能将其重新分配到已有的字符串中。只需声明字符串monthName编码>并在开关中为其指定适当的值
另外,当str
是字符串且1是int
时,您不能打开str
并将1用作大小写标签。简单的解决方法是打开str.substring(0,2)
(以字符串形式表示的月数),并使用“01”
、“02”
等作为案例标签。另一个常见的解决方案是将月号解析为int
(请参阅下面的另一个链接或搜索如何执行),然后保持案例标签1、2等不变
链接
- 解释如何使用
java.time
您好,亲爱的初级开发人员:)
我的建议是:
首先定义月份名称的枚举:
enum MonthName{
January(0),
February(1),
March(2),
April(3),
May(4),
June(5),
July(6),
August(7),
September(8),
October(9),
November(10),
December(11);
private int number;
MonthName(int number){
this.number=number;
}
public static String findName(int number){
for(MonthName monthName:MonthName.values()){
if(monthName.number==number){
return monthName.name();
}
}
throw new RuntimeException("Invalid Month!");
}
}
然后为所需格式定义一个类:
class MyDateFormat{
private int day;
private String month;
private int year;
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public String getMonth() {
return month;
}
public void setMonth(String month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
最后创建一种分离输入字符串的方法(2018年8月15日):
祝你好运:)你好,亲爱的初级开发人员:)
我的建议是:
首先定义月份名称的枚举:
enum MonthName{
January(0),
February(1),
March(2),
April(3),
May(4),
June(5),
July(6),
August(7),
September(8),
October(9),
November(10),
December(11);
private int number;
MonthName(int number){
this.number=number;
}
public static String findName(int number){
for(MonthName monthName:MonthName.values()){
if(monthName.number==number){
return monthName.name();
}
}
throw new RuntimeException("Invalid Month!");
}
}
然后为所需格式定义一个类:
class MyDateFormat{
private int day;
private String month;
private int year;
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public String getMonth() {
return month;
}
public void setMonth(String month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
最后创建一种分离输入字符串的方法(2018年8月15日):
祝你好运:)请试试这个
try {
String str ="8/15/2018";
Date date=new SimpleDateFormat("MM/dd/yyyy").parse(str);
System.out.println("Hello! This is day " + new SimpleDateFormat("dd").format(date) +" of the month of " + new SimpleDateFormat("MMMM").format(date) +" in the year of our Lord " + new SimpleDateFormat("yyyy").format(date));
} catch (ParseException e) {
e.printStackTrace();
}
请试试这个
try {
String str ="8/15/2018";
Date date=new SimpleDateFormat("MM/dd/yyyy").parse(str);
System.out.println("Hello! This is day " + new SimpleDateFormat("dd").format(date) +" of the month of " + new SimpleDateFormat("MMMM").format(date) +" in the year of our Lord " + new SimpleDateFormat("yyyy").format(date));
} catch (ParseException e) {
e.printStackTrace();
}
这段代码在做什么?将字符串分配给方法调用的结果?对于生产代码,可以使用标准库中的LocalDate
和DateTimeFormatter
。这对您来说合适吗,或者您确实是在没有标准类的情况下进行练习以获得编程经验吗?str.substring(0,2)=“一月”
-这行不通。为什么不构建一个全新的结果字符串,而不是摆弄输入字符串?您希望str.substring(0,2)=……
做什么?除了字符串是不可变的,Java是按值返回的(因此尝试将返回值放在=
的左侧可能无法满足您的需要),您似乎试图将最多13个字符压缩到一个只有3个字符的位置。您需要一个新变量。我只想将月份(数字)更改为字符串,如8 is August(对不起,我的英语不好)。这段代码在做什么?将字符串分配给方法调用的结果?对于生产代码,可以使用标准库中的LocalDate
和DateTimeFormatter
。这对您来说合适吗,或者您确实是在没有标准类的情况下进行练习以获得编程经验吗?str.substring(0,2)=“一月”
-这行不通。为什么不构建一个全新的结果字符串,而不是摆弄输入字符串?您希望str.substring(0,2)=……
做什么?除了字符串是不可变的,Java是按值返回的(因此尝试将返回值放在=
的左侧可能无法满足您的需要),您似乎试图将最多13个字符压缩到一个只有3个字符的位置。您需要一个新变量。我只想将月份(数字)更改为字符串,如8是August(很抱歉我的英语不好),谢谢!我也会尝试其他方法,但我觉得有点麻烦,我会想办法的谢谢!欢迎光临!SimpleDataFormat可以解决大部分问题,但是对于检索月份名称,您应该编写更多代码。您可以使用包含月份名称的字符串数组来代替enum。我们已经在Java中内置了month
enum,无需编写。这个答案使用了可怕的旧日期-时间类,多年前被Java.time类取代。建议在2018年使用它们是拙劣的建议。请看Ole V.V.的文章,没错。我定义的枚举在java中是多余的,谢谢!我也会尝试其他方法,但我觉得有点麻烦,我会想办法的谢谢!欢迎光临!SimpleDat