Simpledateformat Producing Wrong Date Time When Parsing "Yyyy-Mm-Dd Hh:Mm"

SimpleDateFormat producing wrong date time when parsing YYYY-MM-dd HH:mm

YYYY should be yyyy-

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.ENGLISH);

Please check the documentation for SimpleDateFormat here

Java 6 : http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

Java 7 : http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

simpledate formater to format returns incorrect.deducted date after formating

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 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);
}
}

Wrong output with Java SimpleDateFormat

D is the day in the year, d is the day of the month. You want YYY/dd/MM https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Wrong value for hour, minutes and mills when parsing date string to yyyy-MM-dd'T'HH:mm:ss'-'hh:mm

If you not need "-04:00" zone offset (timezone) then just truncate string "2015-04-13T10:17:00-04:00" to "2015-04-13T10:17:00"

        String fullDate = "2015-04-13T10:17:00-04:00";
String truncatedDate = fullDate.substring(0, fullDate.lastIndexOf('-'));

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss",
Locale.ENGLISH);
try {

Date dte = format.parse(truncatedDate);
System.out.println("date=" + dte);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Result is:

date=Mon Apr 13 10:17:00 CEST 2015

Java convert string to date not converting correctly

You were using the wrong date format mask. From the documentation, Y corresponds to the week year, and D is the day in year.

Try this version:

Date dateCommence = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.parse("2021-01-06 00:00:00");
System.out.println(dateCommence);

This prints:

Wed Jan 06 00:00:00 CET 2021

Why I get a wrong result when parsing a date from string with SimpleDateFormat ? (Java)

        String dateStr = "September 6, 2013 - 10:48";
SimpleDateFormat parser = new SimpleDateFormat(
"MMMM dd, yyyy - hh:mm", Locale.US);
Date date = parser.parse(dateStr);
System.out.println(date);

Use yyyy and not YYYY

Also don't use Date but use LocalDate and LocalDateTime.

Here is how you would do it using LocalDateTime

        String dateStr = "September 6, 2013 - 10:48";
DateTimeFormatter format = DateTimeFormatter.ofPattern(
"MMMM d, y - HH:mm",Locale.US);
LocalDateTime date = LocalDateTime.parse(dateStr,format);
System.out.println(date.format(format));

Note the HH is for 24 hour time since you didn't include an AM or PM in your date string.

Java SimpleDateFormat(yyyy-MM-dd'T'HH:mm:ss'Z') gives timezone as IST

You haven't set the timezone only added a Z to the end of the date/time, so it will look like a GMT date/time but this doesn't change the value.

Set the timezone to GMT and it will be correct.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

simple date format giving wrong month

Change the first format to

SimpleDateFormat tempFormat = new SimpleDateFormat("yyyy-MM-dd");

as DD is the day in the year. 22 is definitely in January

Java Calendar/SimpleDateFormat giving invalid date

Found solution:

"YYYY-MM-DD" should be "yyyy-MM-dd"

Java SimpleDateFormat accepts invalid date. with format 'MM/dd/yyyy' parses '01/01/2021anything'

This happens because it is implemented this way.

SimpleDateFormat will inspect the format, and start to parse each token present in the format, but does not necessarily parse the whole string, according to the docs:

Parses text from the beginning of the given string to produce a date. The method may not use the entire text of the given string.

You shouldn't be using SimpleDateFormat and Date anyways, because they have more problems than this one.

Use classes from the java.time package instead. In your case, you should probably be using LocalDate. A simple translation of your code to code using java.time would be something like this:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
LocalDate d = LocalDate.parse("01/01/2021anything", formatter);
System.out.println(d);

The abovementioned code will throw a DateTimeParseException, because it doesn't like anything.



Related Topics



Leave a reply



Submit