将包含日期的字符串转换为日期类型并格式化它。

huangapple go评论62阅读模式
英文:

can you help me with converting this String that contains a date to a varialble of type date and format it?

问题

I have this String that contains a date in this format "2023-04-20T00:00:00+02:00" and I want to convert it back to date in a format just like this "2023-04-20".

I tried this code:

String dateString = obj.get("eventDate").toString();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse(dateString.substring(0, 10));
t.setEventDate(date);

But it gave me this output: "Wed Apr 26 01:00:00 WAT 2023," and it's not what I'm looking for.

英文:

i have this String that contains a date in this format "2023-04-20T00:00:00+02:00" and i want to convert it back to date in a format just like this 2023-04-20

i tried this code

 String dateString=obj.get("eventDate").toString();
 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 Date date = dateFormat.parse(dateString.substring(0, 10));     
 t.setEventDate(date);

but it gave me this output Wed Apr 26 01:00:00 WAT 2023 and it's not what am looking for

答案1

得分: 3

将您拥有的String转换为Date对象,基于输入格式...

String dateString = "2023-04-20T00:00:00+02:00";
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
Date date = inFormat.parse(dateString);

然后,使用不同的格式化程序将Date格式化回所需的String表示:

SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
String outString = outFormat.format(date);
System.out.println(outString);

这将输出2023-04-20

请记住,日期/时间类是对某个时间锚点(例如Unix Epoch)经过的时间量的表示,它们没有固有的“格式”概念。实际上,这些类的输出应被视为调试信息,这就是为什么我们需要格式化程序的原因。

现代方法

您应该避免使用java.util日期/时间类,它们已被弃用,而应该使用java.timeAPI:

String dateString = "2023-04-20T00:00:00+02:00";
LocalDateTime ldt = LocalDateTime.parse(dateString, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
String outString = ldt.format(DateTimeFormatter.ISO_DATE);
System.out.println(outString);
英文:

Convert the String you have to a Date object, based on the input format...

String dateString = "2023-04-20T00:00:00+02:00";
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
Date date = inFormat.parse(dateString);

Then use a different formatter to format the Date back to the desired String representation

SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
String outString = outFormat.format(date);
System.out.println(outString);

Which will output 2023-04-20

Remember, date/time classes are a representation of the amount of time which has passed since some anchor point in time (ie the Unix Epoch), they do not have an intrinsic concept of "format", in fact the output of these classes should be consider debug information only, this is why we have formatters.

The modern approach

You should avoid making use of the java.util date/time classes, they are effectively deprecated, instead, you should be making use of the java.time API instead

String dateString = "2023-04-20T00:00:00+02:00";
LocalDateTime ldt = LocalDateTime.parse(dateString, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
String outString = ldt.format(DateTimeFormatter.ISO_DATE);
System.out.println(outString);

答案2

得分: 0

OffsetDateTime
.parse("2023-04-20T00:00:00+02:00")
.toLocalDate()
.toString();
String result = "2023-04-20T00:00:00+02:00".substring(0, 10);
String result = "2023-04-20T00:00:00+02:00".split("T")[0];
OffsetDateTime odt = OffsetDateTime.parse("2023-04-20T00:00:00+02:00");
LocalDate ld = odt.toLocalDate();
String result = ld.toString();
英文:

tl;dr

OffsetDateTime
.parse( "2023-04-20T00:00:00+02:00" )
.toLocalDate()
.toString()

See this code run at Ideone.com.

>2023-04-20

String manipulation

Obviously you could simply manipulate the input string. Truncate after the first ten letters.

String result = "2023-04-20T00:00:00+02:00".substring( 0 , 10 ) ;

Or, split the string into pieces. Grab the first piece.

String result = "2023-04-20T00:00:00+02:00".split( "T" )[ 0 ] ;

java.time

You could parse the input as a date-time value.

Use only the java.time classes for date-time work. Never use the terribly flawed legacy classes such as Date, Calendar, and SimpleDateFormat.

To parse your particular input, use the java.time.OffsetDateTime class. Your input consists of three parts: a date, a time-of-day, and an offset from UTC of a number of hours and minutes. To OffsetDateTime class fully represents all three parts.

OffsetDateTime odt = OffsetDateTime.parse( "2023-04-20T00:00:00+02:00" ) ;

In contrast, note that the LocalDateTime class seen in the other Answer cannot represent all three parts. That class represents only the first two of the three parts. So the use of that class there is misleading and confusing.

After parsing, we can extract just the date portion, without the time of day, and without the offset.

LocalDate ld = odt.toLocalDate() ;

Generate text to represent that date value, in standard ISO 8601 format.

String result = ld.toString() ;

huangapple
  • 本文由 发表于 2023年5月7日 10:12:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定