I am trying to convert a string to date using java 8 to a certain format. Below is my code. Even after mentioning the format pattern as MM/dd/yyyy the output I am receiving is yyyy/DD/MM format. Can somebody point out what I am doing wrong?
我正在尝试使用java 8将字符串转换为特定格式的日期。以下是我的代码。即使在将格式模式提到MM / dd / yyyy后,我收到的输出也是yyyy / DD / MM格式。有人可以指出我做错了什么吗?
String str = "01/01/2015";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
LocalDate dateTime = LocalDate.parse(str, formatter);
System.out.println(dateTime);
3 个解决方案
#1
5
That is because you are using the toString method which states that:
那是因为您正在使用toString方法,该方法声明:
The output will be in the ISO-8601 format uuuu-MM-dd.
输出将采用ISO-8601格式uuuu-MM-dd。
The DateTimeFormatter
that you passed to LocalDate.parse
is used just to create a LocalDate
, but it is not "attached" to the created instance. You will need to use LocalDate.format
method like this:
传递给LocalDate.parse的DateTimeFormatter仅用于创建LocalDate,但它不会“附加”到创建的实例。您需要使用LocalDate.format方法,如下所示:
String str = "01/01/2015";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
LocalDate dateTime = LocalDate.parse(str, formatter);
System.out.println(dateTime.format(formatter)); // not using toString
#2
2
LocalDate is a Date Object. It's not a String object so the format in which it will show the date output string will be dependent on toString implementation.
LocalDate是一个日期对象。它不是String对象,因此它将显示日期输出字符串的格式将取决于toString实现。
You have converted it correctly to LocalDate object but if you want to show the date object in a particular string format, you need to format it accordingly:
您已将其正确转换为LocalDate对象,但如果要以特定字符串格式显示日期对象,则需要对其进行相应格式化:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
System.out.println(dateTime.format(formatter))
This way you can convert date to any string format you want by providing formatter.
这样,您可以通过提供格式化程序将日期转换为您想要的任何字符串格式。
#3
1
You can use SimpleDateFormat class for that purposes. Initialize SimpleDateFormat object with date format that you want as a parameter.
您可以将SimpleDateFormat类用于此目的。使用您想要作为参数的日期格式初始化SimpleDateFormat对象。
String dateInString = "27/02/2016"
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(dateInString);
#1
5
That is because you are using the toString method which states that:
那是因为您正在使用toString方法,该方法声明:
The output will be in the ISO-8601 format uuuu-MM-dd.
输出将采用ISO-8601格式uuuu-MM-dd。
The DateTimeFormatter
that you passed to LocalDate.parse
is used just to create a LocalDate
, but it is not "attached" to the created instance. You will need to use LocalDate.format
method like this:
传递给LocalDate.parse的DateTimeFormatter仅用于创建LocalDate,但它不会“附加”到创建的实例。您需要使用LocalDate.format方法,如下所示:
String str = "01/01/2015";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
LocalDate dateTime = LocalDate.parse(str, formatter);
System.out.println(dateTime.format(formatter)); // not using toString
#2
2
LocalDate is a Date Object. It's not a String object so the format in which it will show the date output string will be dependent on toString implementation.
LocalDate是一个日期对象。它不是String对象,因此它将显示日期输出字符串的格式将取决于toString实现。
You have converted it correctly to LocalDate object but if you want to show the date object in a particular string format, you need to format it accordingly:
您已将其正确转换为LocalDate对象,但如果要以特定字符串格式显示日期对象,则需要对其进行相应格式化:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
System.out.println(dateTime.format(formatter))
This way you can convert date to any string format you want by providing formatter.
这样,您可以通过提供格式化程序将日期转换为您想要的任何字符串格式。
#3
1
You can use SimpleDateFormat class for that purposes. Initialize SimpleDateFormat object with date format that you want as a parameter.
您可以将SimpleDateFormat类用于此目的。使用您想要作为参数的日期格式初始化SimpleDateFormat对象。
String dateInString = "27/02/2016"
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(dateInString);