【问题标题】:Can we change Default time provide by spring in SimpleDateFormat我们可以在 SimpleDateFormat 中更改 spring 提供的默认时间吗
【发布时间】:2021-12-08 19:10:29
【问题描述】:
    this.mdbReading.y = new Date[reading.y.length];
    SimpleDateFormat ydateFormat = new SimpleDateFormat("yyyy-MM-dd");
    
    for ( i = 0; i < reading.y.length; i++) {    
        //System.out.println("i = " + i + " string is "+ reading.y[i]);                                   
        try {
            
            this.mdbReading.y[i] = ydateFormat.parse(reading.y[i]);
        } catch (Exception e) {
            //TODO: handle exception
            System.out.println("Exception in date parse "+ e);
            break;
        }
    }

这是我的代码,其中输入字符串,即reading.y[i] 的格式为“2016-04-28”......我得到的输出为“2016-04-24 T18.30:00 00:00:00”虽然我期望输出为“2016-04-24 T00.000:00 00:00:00”..所以有没有办法将默认时间设置为 00:00?

【问题讨论】:

  • 我建议你不要使用SimpleDateFormatDate。这些类设计不佳且早已过时,尤其是前者,尤其是出了名的麻烦。而是使用来自java.time, the modern Java date and time APILocalDateDateTimeFormatter。查看答案。
  • Anti-pattern: parallel collections。你还没有发布完整的代码,所以很难确定。我觉得我闻到了糟糕的设计。如果是这样,该链接应该会有所帮助。

标签: date datetime spring-mvc spring-data simpledateformat


【解决方案1】:

java.time

java.util 日期时间 API 及其格式化 API SimpleDateFormat 已过时且容易出错。建议完全停止使用,改用modern Date-Time API*

使用现代日期时间 API java.time 的解决方案:

import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class Main {
    public static void main(String[] args) throws ParseException {
        LocalDate date = LocalDate.parse("2016-04-28");
        System.out.println(date);

        // In case you also need time-part as 00:00
        LocalDateTime ldt = date.atTime(LocalTime.MIN);
        System.out.println(ldt);
    }
}

输出:

2016-04-28
2016-04-28T00:00

现代日期时间 API 基于 ISO 8601,只要日期时间字符串符合 ISO 8601 标准,就不需要明确使用 DateTimeFormatter 对象。您的日期字符串已采用 ISO 8601 格式。

Trail: Date Time 了解有关现代日期时间 API 的更多信息。查看this answerthis answer 了解如何将java.time API 与JDBC 结合使用。

您的代码出了什么问题?

默认SimpleDateFormat 在解析任何字符串时使用默认时区。下面给出的是重现您的错误的代码:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) throws ParseException {
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Kolkata"));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(sdf.parse("2016-04-28").toInstant());
    }
}

输出:

2016-04-27T18:30:00Z

为了保留00:00:00 的时间部分,您需要将UTC 时区设置为SimpleDateFormat 实例。

演示:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        System.out.println(sdf.parse("2016-04-28").toInstant());
    }
}

输出:

2016-04-28T00:00:00Z

* 如果您正在为一个 Android 项目工作并且您的 Android API 级别仍然不符合 Java-8,请检查 Java 8+ APIs available through desugaring。请注意,Android 8.0 Oreo 已经提供了support for java.time

【讨论】:

    猜你喜欢
    • 2018-03-21
    • 2019-07-18
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多