如何从字符串日期更改日期格式 [英] how to change format of date from string date

查看:59
本文介绍了如何从字符串日期更改日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将日期作为这样的字符串

I have date as a string like this

String date = "11-12-2018"

我想将其更改为"2018-12-11"

具有相同的变量.因此,我尝试了下面的代码,但没有得到预期的输出.

with the same variable. So, I tried the code below but it doesn't give me the output I expect.

String date = "11-12-2018"

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

Date d = df.parse(date);

导致:"0012-06-09"

results in: "0012-06-09"

我想要"2018-12-11"

I want "2018-12-11"

推荐答案

您可以通过3种方法进行操作.第一种是使用 SimpleDateFormat Date ,第二种是使用 DateTimeFormatter LocalDate ,第三种是使用 Split .

You can do this 3 ways. First is using SimpleDateFormat and Date and second using DateTimeFormatter and LocalDate and third you can use Split.

1.使用Date和SimpleDateFormat

String date = "11-12-2018";
SimpleDateFormat df = new SimpleDateFormat("dd-mm-yyyy");
java.util.Date d = df.parse(date);
String finalDate = new SimpleDateFormat("yyyy-MM-dd").format(d);
System.out.println(finalDate);

在这里,我们有实际的日期 String date ="11-12-2018"; 我们知道我们想将其更改为 2018-12-11

Here we have our actual date String date = "11-12-2018"; we know we want to change it to 2018-12-11

因此,我们可以使用此代码将该日期解析为 Date 对象

So lets parse that date into a Date object using this code

SimpleDateFormat df = new SimpleDateFormat("dd-mm-yyyy");
java.util.Date d = df.parse(date);

好吧,现在我们有了一个实际日期的日期对象,现在让我们将其格式化为新日期.

Okay so now we have a date object of our actual date, Now lets format it to our new date.

String finalDate = new SimpleDateFormat("yyyy-MM-dd").format(d);

2.使用LocalDate和DateTimeFormatter

好的,在这里我们再次定义日期和2 DateTimeFormatter.

Alright here we define our date again and 2 DateTimeFormatter.

DateTimeFormatter oldFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

第一个格式化程序是我们的旧日期格式,第二个是我们将旧日期转换成的新格式.

The first formatter is our old date format, and the second one is the new one that we are gonna convert the old date into.

好的,现在就使用它们吧!

Alright lets use them now!

现在,我们通过使用 oldFormatter 对象

Now we make a new LocalDate object using our oldFormatter by parsing our dateString with the oldFormatter object

LocalDate dateTime = LocalDate.parse(date, oldFormatter);

现在让我们对其进行格式化.

Alright now lets format it.

String reformattedDate = dateTime.format(newFormatter);

就这么简单!这是完整的代码.

as simple as that! Here is the full code.

String date = "11-12-2018";
DateTimeFormatter oldFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate dateTime = LocalDate.parse(date, oldFormatter);
String reformattedDate = dateTime.format(newFormatter);
System.out.println(reformattedDate);

3.使用String :: Split

好的,这部分非常简单.让我们使用-

Okay this part is pretty simple. Lets split the date using -

String[] dates = date.split("-");

我们已经知道日期的顺序可以使用 String :: format

We already know the order of the date lets format it using String::format

String reformattedDate = String.format("%s-%s-%s", dates[2], dates[1], dates[0]);

这是完整的代码

String date = "11-12-2018";
String[] dates = date.split("-");
String reformattedDate = String.format("%s-%s-%s", dates[2], dates[1], dates[0]);
System.out.println(reformattedDate);

这篇关于如何从字符串日期更改日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆