simpledate formater to format returns incorrect.deducted date after formating

huangapple go评论67阅读模式
英文:

simpledate formater to format returns incorrect.deducted date after formating

问题

以下是翻译好的内容:

我正在尝试使用SimpleDateFormat来解析日期。日期成功解析,但输出的日期格式不正确,或者减少了一年。使用SimpleDateFormat的Date方法如下所示:

public Date parseDate(String date) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
    Date parsedDate = null;
    try {
        parsedDate = format.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return parsedDate;
}

这是解析的示例图像:

Date start = parseDate(dateVal); // dateVal示例为2020-09-25

当解析日期时,输出如下:

Sun Dec 29 00:00:00 WAT 2019

我的挑战是在解析后返回日期示例 - 2020-09-25。

英文:

I am attempting to parse date using SimpleDateFormat. The date is parsed successfully but the output date format is incorrect or deducted by a year, The Date method that uses SimpleDateFormat is shown below

public Date parseDate(String date) {
        SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-DD",Locale.ENGLISH);
        Date parsedDate = null;
        try {
            parsedDate = format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parsedDate;
    }

Here is the sample image parsed

Date start = parseDate(dateVal); // dateVal sample is 2020-09-25

When the date is parsed, this is the output

Sun Dec 29 00:00:00 WAT 2019 

My challenge is to return a date sample of - 2020-09-25 after parsing

答案1

得分: 2

你可以这样做:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
parsedDate = format.parse(date);

你应该使用parse(String),而不是parseDate()

英文:

You can do it like so:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
parsedDate = format.parse(date);

You should be using parse(String) instead of parseDate()

答案2

得分: 2

你使用了错误的符号。请使用 yyyy-MM-dd 替代 YYYY-MM-DD。注意,DD 代表年中的天数Y 代表周年。详细信息请查阅文档

另外,我建议你从过时且容易出错的 java.util 日期时间 API 和 SimpleDateFormat 切换到现代的 java.time 日期时间 API 以及相应的格式化 API(位于包 java.time.format 中)。你可以从**教程:日期时间**中了解更多关于现代日期时间 API 的信息。

如果你在使用 Android,且你的 Android API 版本仍不支持 Java 8,请查阅 https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project 以及 通过 desugaring 可用的 Java 8+ API

使用现代日期时间 API:

import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.parse("2020-09-25");
        System.out.println(date);
    }
}

输出:

2020-09-25

注意,由于你的日期时间字符串已经处于 ISO8601 格式中,你在将其解析为 LocalDate 时无需使用任何 DateTimeFormatter,因为这是 LocalDate#parse 默认使用的模式。

使用传统的日期时间 API:

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

public class Main {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse("2020-09-25");
        System.out.println(date);
    }
}
英文:

You have used the wrong symbols. Use yyyy-MM-dd instead of YYYY-MM-DD. Note that DD stands for the Day of the year and Y stands for Week year. Check the documentation to learn more about it.

Also, 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.

If you are using Android and your Android API level is still not compliant with Java8, check https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project and Java 8+ APIs available through desugaring.

Using modern date-time API:

import java.time.LocalDate;

public class Main {
	public static void main(String[] args) {
		LocalDate date = LocalDate.parse("2020-09-25");
		System.out.println(date);
	}
}

Output:

2020-09-25

Note that since your date-time string is already in the ISO8601 format, you do not need to use any DateTimeFormatter while parsing it to LocalDate as it is the default pattern used by LocalDate#parse.

Using the legacy date-time API:

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

public class Main {
	public static void main(String[] args) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse("2020-09-25");
		System.out.println(date);
	}
}

huangapple
  • 本文由 发表于 2020年9月26日 05:48:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64071686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定