当字符串具有"TRT"时,如何将字符串转换为日期?在里面 [英] How can i convert String to Date when it has "TRT" in it
问题描述
String sDate = "06.08.2020" // 06 day 08 month 2020 is year
这是我在txt文件中拥有的日期.我在JTable中使用它们.为了对表格进行排序,我使用此DateFormatter将它们转换为日期.
This is the date i have in my txt file. I use them in JTable. To sort the table i convert them to date with this DateFormatter.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
它确实将字符串转换为日期.
And it does convert the string to date as this.
LocalDate date = LocalDate.parse(sDate,formatter);
//The date : Thu Aug 06 00:00:00 TRT 2020
现在,我需要像第一个日期 06.08.2020
一样对其进行转换.但是我不能使用 date 作为输入.因为我是从JTable中获取的,所以我以 String 的形式获取它.
Now i need to convert it like the first date 06.08.2020
.
But i can't use date as input. Because i get it from JTable so i get it as String.
所以我尝试了这段代码.
So i tryed this code.
String sDate1 = "Thu Aug 06 00:00:00 TRT 2020";// The date i get from JTable
LocalDate lastdate = LocalDate.parse(sDate1,formatter);
sDate1 = formatter.format(lastdate);
但是我收到一个错误,因为无法在索引0处解析此文本'Thu Aug 06 00:00:00 TRT 2020'.
But i get an error as this Text 'Thu Aug 06 00:00:00 TRT 2020' could not be parsed at index 0
.
所以这个圆锥体无法正常工作: LocalDate lastdate = LocalDate.parse(sDate1,formatter);
我看不出问题出在哪里.
So this cone not works fine : LocalDate lastdate = LocalDate.parse(sDate1,formatter);
I cant see where is the problem.
推荐答案
代码失败的地方:
SimpleDateFormat sdf1=new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
String dateStr = "06.08.2020";
sdf1.parse(dateStr);
如您所见, SimpleDateFormat
的模式与 date
字符串的模式不匹配,因此,此代码将引发 ParseException
.
As you can see, the pattern of the SimpleDateFormat
and that of the date
string do not match and therefore, this code will throw ParseException
.
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String dateStr = "06.08.2020";
Date date = sdf.parse(dateStr);
您必须已经知道它为什么起作用.之所以起作用,是因为 SimpleDateFormat
的模式与 dateStr
字符串的模式匹配.
You must have already got why it worked. It worked because the pattern of the SimpleDateFormat
matches with that of the dateStr
string.
是的,只需使用与解析原始字符串相同的格式,如下所示:
Yes, just use the same format which you used to parse the original string as shown below:
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String dateStr = "06.08.2020";
Date date = sdf.parse(dateStr);
// Display in the default format
System.out.println(date);
// Format into the string
dateStr = sdf.format(date);
System.out.println(dateStr);
一条建议:
我建议您从过时且容易出错的 java.util
日期时间API和 SimpleDateFormat
切换到跟踪:日期时间 .
A piece of advice:
I recommend you switch from the outdated and error-prone java.util
date-time API and SimpleDateFormat
to the modern java.time
date-time API and the corresponding formatting API (package, java.time.format
). Learn more about the modern date-time API from Trail: Date Time.
使用现代的日期时间API:
Using the modern date-time API:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
String dateStr = "06.08.2020";
LocalDate date = LocalDate.parse(dateStr, formatter);
// Display in the default format
System.out.println(date);
// Format into the string
dateStr = formatter.format(date);
System.out.println(dateStr);
使用旧版API和现代API没什么区别:
对于这个简单的示例来说确实如此,但是当您需要使用日期和时间执行复杂的操作时,您会发现现代的日期时间API既聪明又干净,而传统的API复杂且容易出错.
I don't see any difference using the legacy API and the modern API:
That's true for this simple example but when you will need to do complex operations using date and time, you will find the modern date-time API smart and clean while the legacy API complex and error-prone.
演示:
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// Given date-time string
String strDate = "Thu Aug 06 00:00:00 TRT 2020";
// Replace TRT with standard time-zone string
strDate = strDate.replace("TRT", "Europe/Istanbul");
// Define formatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzzz yyyy");
// Parse the date-time string into ZonedDateTime
ZonedDateTime zdt = ZonedDateTime.parse(strDate, formatter);
System.out.println(zdt);
// If you wish, convert ZonedDateTime into LocalDateTime
LocalDateTime ldt = zdt.toLocalDateTime();
System.out.println(ldt);
}
}
输出:
2020-08-06T00:00+03:00[Europe/Istanbul]
2020-08-06T00:00
这篇关于当字符串具有"TRT"时,如何将字符串转换为日期?在里面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!