当其中包含 "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 中使用它们。为了对 table 进行排序,我使用此 DateFormatter 将它们转换为日期。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
它确实将字符串转换为日期。
LocalDate date = LocalDate.parse(sDate,formatter);
//The date : Thu Aug 06 00:00:00 TRT 2020
现在我需要像第一个日期那样转换它 06.08.2020
。
但我不能使用 date 作为输入。因为我从 JTable 得到它所以我得到它作为 String.
所以我尝试了这个代码。
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);
但是我得到一个错误 Text 'Thu Aug 06 00:00:00 TRT 2020' could not be parsed at index 0
。
所以这个锥体不能正常工作:LocalDate lastdate = LocalDate.parse(sDate1,formatter);
我看不出哪里出了问题。
我无法重现您描述的行为。以下代码对我来说很好用:
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);
}
}
输出: 日期 = 06.08.2020
也就是说,如果可能的话,您应该切换到新的 java.time.*
API。
您的代码失败的地方:
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);
// Display in the default format
System.out.println(date);
// Format into the string
dateStr = sdf.format(date);
System.out.println(dateStr);
一条建议:
我建议您从过时的 error-prone java.util
date-time API 和 SimpleDateFormat
切换到 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.
使用现代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 有什么区别:
对于这个简单的例子来说确实如此,但是当您需要使用日期和时间进行复杂的操作时,您会发现现代的 date-time API 智能而干净,而传统的 API 复杂且 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
String sDate = "06.08.2020" // 06 day 08 month 2020 is year
这是我的 txt 文件中的日期。我在 JTable 中使用它们。为了对 table 进行排序,我使用此 DateFormatter 将它们转换为日期。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
它确实将字符串转换为日期。
LocalDate date = LocalDate.parse(sDate,formatter);
//The date : Thu Aug 06 00:00:00 TRT 2020
现在我需要像第一个日期那样转换它 06.08.2020
。
但我不能使用 date 作为输入。因为我从 JTable 得到它所以我得到它作为 String.
所以我尝试了这个代码。
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);
但是我得到一个错误 Text 'Thu Aug 06 00:00:00 TRT 2020' could not be parsed at index 0
。
所以这个锥体不能正常工作:LocalDate lastdate = LocalDate.parse(sDate1,formatter);
我看不出哪里出了问题。
我无法重现您描述的行为。以下代码对我来说很好用:
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);
}
}
输出: 日期 = 06.08.2020
也就是说,如果可能的话,您应该切换到新的 java.time.*
API。
您的代码失败的地方:
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);
// Display in the default format
System.out.println(date);
// Format into the string
dateStr = sdf.format(date);
System.out.println(dateStr);
一条建议:
我建议您从过时的 error-prone java.util
date-time API 和 SimpleDateFormat
切换到 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.
使用现代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 有什么区别:
对于这个简单的例子来说确实如此,但是当您需要使用日期和时间进行复杂的操作时,您会发现现代的 date-time API 智能而干净,而传统的 API 复杂且 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