Java:Cannot Format Given Object as a Date

Java : Cannot format given Object as a Date

DateFormat.format only works on Date values.

You should use two SimpleDateFormat objects: one for parsing, and one for formatting. For example:

// Note, MM is months, not mm
DateFormat outputFormat = new SimpleDateFormat("MM/yyyy", Locale.US);
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX", Locale.US);

String inputText = "2012-11-17T00:00:00.000-05:00";
Date date = inputFormat.parse(inputText);
String outputText = outputFormat.format(date);

EDIT: Note that you may well want to specify the time zone and/or locale in your formats, and you should also consider using Joda Time instead of all of this to start with - it's a much better date/time API.

Java SimpleDateFormat Cannot format given Object as a Date

At the main method, you are sending date as "2022.02.10 17:54:55". However, you wrote format of the pattern as "yyyy-MM-dd HH:mm:ss". If you change string at main as "2022-02-10 17:54:55" or you can change the pattern at the SimpleDateFormat constructor as "yyyy.MM.dd HH:mm:ss".

Java Cannot Format Given Object as Date

You can parse a java.util.Date into a String like this:

    String dateStr = "20190226";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = sdf.parse(dateStr);
sdf = new SimpleDateFormat("dd.MM.yyyy");
dateStr = sdf.format(date);

Cannot format LocaDateTime given Object as a Date

Do not use outdated Date/Time formatter, DateFormat. Use a modern Date/Time formatter, DateTimeFormatter instead.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
System.out.println(formatter.format(LocalDateTime.now().plusMinutes(10)));
}
}

Output:

20200415203347

java.lang.IllegalArgumentException: Cannot format given Object as a Date

Try this

public static void main(String[] args) throws ParseException {
SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdfOut = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String input = "2014-12-09 02:18:38";
Date date = sdfIn.parse(input);

System.out.println(sdfOut.format(date));
}

Also, note that m is for minutes, while M is for months.

Cannot format given Object as a Date error when formatting google api client DateTime

You are getting the error because you are trying to use SimpleDateFormat with wrong Date Object.

You should be using java.util.Date instead if you want to make use of SimpleDateFormat.

Here is the corrected code snippet:

java.util.Date date = new java.util.Date(System.currentTimeMillis());
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String dateAsISOString = df.format(date);
System.out.println(dateAsISOString);

Cannot format given object as a Date using LocaleDateTime.now

Replace DateTimeFormatter instead of SimpleDateFormat

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

class Scratch {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
String result = formatter.format(now);
System.out.println(result);
}
}

Java Localize DateFormat Cannot format given Object as a Date

java.time

    String dateString = "2019-06-01 00:15:00";
dateString = dateString.replace(" ", "T");
dateString = dateString.concat("Z");

DateTimeFormatter formatter = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.MEDIUM)
.withLocale(Locale.FRENCH);
OffsetDateTime dateTime = OffsetDateTime.parse("2019-06-01T00:15:00Z");
String formattedDateTime = dateTime.format(formatter);

System.out.println("Formatted date/time: " + formattedDateTime);

Output:

Formatted date/time: 01/06/2019 00:15:00

I recommend you don’t use DateFormat. That class is notoriously troublesome and long outdated. Instead use DateTimeFormatter from java.time, the modern Java date and time API, as shown in my code.

For what went wrong in your code please see the similar question that I link to at the bottom.

Links

  • Oracle tutorial: Date Time explaining how to use java.time.
  • Similar question: Java : Cannot format given Object as a Date. My answer to the same.


Related Topics



Leave a reply



Submit