I am clearly missing something but I don't know what. I wrote this example program that replicates a problem I'm having:
import java.util.Date;
import java.text.SimpleDateFormat;
public class TestDate {
public static void main(String... args) throws Exception {
// The input date format
SimpleDateFormat iF = new SimpleDateFormat("MMM dd YYYY HH:mm");
// The output date format
SimpleDateFormat oF = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ssXXX");
// Parse an input date and format it
System.out.println(oF.format(iF.parse("Aug 26 2020 18:00")));
}
}
When I run it, the output is this:
2020-12-29T18:00:00-05:00
The time and time zone are correct, but why is the date December 29 when I clearly input August 26?
Please refer to the SimpleDateFormat documentation and see if you can figure out what I am doing wrong. I've tried using L
in place of M
like the documentation suggests, but I get the same result.
Use yyyy instead YYYY:
public static void main(String... args) throws Exception {
// The input date format
SimpleDateFormat iF = new SimpleDateFormat("MMM dd yyyy HH:mm", Locale.US);
// The output date format
SimpleDateFormat oF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
// Parse an input date and format it
System.out.println(oF.format(iF.parse("Aug 26 2020 18:00")));
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments