英文:
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
这是我在文本文件中有的日期。我在JTable中使用它们。为了对表格进行排序,我使用以下的日期格式化程序将它们转换为日期。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
它确实将字符串转换为日期,如下所示。
LocalDate date = LocalDate.parse(sDate, formatter);
// 日期:2020年8月6日,星期四,00:00:00 TRT
现在我需要将它转换为第一个日期 `06.08.2020` 的格式。
但是我不能使用 *date* 作为输入。因为我从JTable中获取它,所以我将其作为 *String* 获取。
因此,我尝试了这段代码。
String sDate1 = "Thu Aug 06 00:00:00 TRT 2020"; // 我从JTable中获取的日期
LocalDate lastdate = LocalDate.parse(sDate1, formatter);
sDate1 = formatter.format(lastdate);
但我收到一个错误,内容如下:`无法解析文本 'Thu Aug 06 00:00:00 TRT 2020'(索引 0 处开始)。`
所以这段代码不正确:`LocalDate lastdate = LocalDate.parse(sDate1, formatter);`
我看不出问题出在哪里。
英文:
String sDate = "06.08.2020" // 06 day 08 month 2020 is year
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
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);
But i get an error as this Text 'Thu Aug 06 00:00:00 TRT 2020' could not be parsed at index 0
.
So this cone not works fine : LocalDate lastdate = LocalDate.parse(sDate1,formatter);
I cant see where is the problem.
答案1
得分: 3
我无法复现您所描述的行为。以下代码在我的机器上运行良好:
import java.util.*;
import java.text.*;
public class MyClass {
public static void main(String args[]) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String date = "06.08.2020";
Date date1 = sdf.parse(date);
String result = sdf.format(date1);
System.out.println("Date = " + result);
}
}
输出: Date = 06.08.2020
话虽如此,如果有可能的话,您应该切换到新的 java.time.*
API。
英文:
I cannot reproduce the behaviour you describe. The following code worked fine for me:
import java.util.*;
import java.text.*;
public class MyClass {
public static void main(String args[]) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String date = "06.08.2020";
Date date1 = sdf.parse(date);
String result = sdf.format(date1);
System.out.println("Date = " + result);
}
}
Output: Date = 06.08.2020
That being said, if at all possible you should switch to the new java.time.*
API.
答案2
得分: 2
代码失败的地方:
SimpleDateFormat sdf1=new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
String dateStr = "06.08.2020";
sdf1.parse(dateStr);
正如你所看到的,SimpleDateFormat
的模式和 date
字符串的模式不匹配,因此,这段代码会抛出 ParseException
。
如何使其工作?
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String dateStr = "06.08.2020";
Date date = sdf.parse(dateStr);
你可能已经明白为什么它起作用了。这段代码之所以有效,是因为 SimpleDateFormat
的模式与 dateStr
字符串的模式匹配。
我能将 Date
对象(即 date
)格式化为原始字符串吗?
是的,只需使用与解析原始字符串时相同的格式,如下所示:
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String dateStr = "06.08.2020";
Date date = sdf.parse(dateStr);
// 以默认格式显示
System.out.println(date);
// 格式化为字符串
dateStr = sdf.format(date);
System.out.println(dateStr);
一些建议:
我建议你从过时且容易出错的 java.util
日期时间 API 和 SimpleDateFormat
切换到现代的 java.time
日期时间 API,以及相应的格式化 API(包,java.time.format
)。从**Trail: Date Time**了解有关现代日期时间 API 的更多信息。
使用现代日期时间 API:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
String dateStr = "06.08.2020";
LocalDate date = LocalDate.parse(dateStr, formatter);
// 以默认格式显示
System.out.println(date);
// 格式化为字符串
dateStr = formatter.format(date);
System.out.println(dateStr);
我看不出使用传统 API 和现代 API 有什么区别:
对于这个简单的示例来说确实如此,但当你需要使用日期和时间进行复杂操作时,你会发现现代日期时间 API 智能而干净,而传统 API 复杂且容易出错。
演示:
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// 给定的日期时间字符串
String strDate = "Thu Aug 06 00:00:00 TRT 2020";
// 将 TRT 替换为标准时区字符串
strDate = strDate.replace("TRT", "Europe/Istanbul");
// 定义格式化器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzzz yyyy");
// 将日期时间字符串解析为 ZonedDateTime
ZonedDateTime zdt = ZonedDateTime.parse(strDate, formatter);
System.out.println(zdt);
// 如果你愿意,将 ZonedDateTime 转换为 LocalDateTime
LocalDateTime ldt = zdt.toLocalDateTime();
System.out.println(ldt);
}
}
输出:
2020-08-06T00:00+03:00[Europe/Istanbul]
2020-08-06T00:00
英文:
Where your code failed:
SimpleDateFormat sdf1=new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
String dateStr = "06.08.2020";
sdf1.parse(dateStr);
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
.
How to make it work?
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
String dateStr = "06.08.2020";
Date date = sdf.parse(dateStr);
You must have already got why it worked. It worked because the pattern of the SimpleDateFormat
matches with that of the dateStr
string.
Can I format the Date
object (i.e. date
) into the original 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);
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.
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);
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.
Demo:
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);
}
}
Output:
2020-08-06T00:00+03:00[Europe/Istanbul]
2020-08-06T00:00
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论