当其中包含“ TRT”时,如何将字符串转换为日期

问题描述

String sDate = "06.08.2020" // 06 day 08 month 2020 is year

这是我在txt文件中拥有的日期。我在JTable中使用它们。为了对表格进行排序,我使用此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);

如您所见,SimpleDateFormatdate字符串的模式不匹配,因此此代码将抛出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);

一条建议:

我建议您从过时且容易出错的java.util日期时间API和SimpleDateFormat切换到modern 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);

// 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复杂且容易出错。

演示:

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