Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java : Cannot format given Object as a Date

Tags:

java

I have Date in this format (2012-11-17T00:00:00.000-05:00). I need to convert the date into this format mm/yyyy.

I tried this way, but I am getting this Exception.

Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date     at java.text.DateFormat.format(Unknown Source)     at java.text.Format.format(Unknown Source)     at DateParser.main(DateParser.java:14) 

Please see my code below:

import java.text.SimpleDateFormat; import java.util.Date;  public class DateParser {       public static void main(String args[]) {        String MonthYear = null;         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm/yyyy");         String dateformat = "2012-11-17T00:00:00.000-05:00";     MonthYear = simpleDateFormat.format(dateformat);         System.out.println(MonthYear);       }     } 
like image 1000
Pawan Avatar asked May 18 '12 09:05

Pawan


People also ask

How can I change the format of Date object in Java?

String mydateStr = "/107/2013 12:00:00 AM"; DateFormat df = new SimpleDateFormat("/dMM/yyyy HH:mm:ss aa"); Date mydate = df. parse(mydateStr); Two method above can be used to change a formatted date string from one into the other. See the javadoc for SimpleDateFormat for more info about formatting codes.

How do you format a date in Java?

Java SimpleDateFormat with Locale String pattern = "EEEEE MMMMM yyyy HH:mm:ss. SSSZ"; SimpleDateFormat simpleDateFormat =new SimpleDateFormat(pattern, new Locale("fr", "FR")); String date = simpleDateFormat. format(new Date()); System.

Does Java have a date format?

The DateFormat class in Java is used for formatting dates. A specified date can be formatted into the Data/Time string. For example, a date can be formatted into: mm/dd/yyyy. Date Format classes are not synchronized.


2 Answers

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.

like image 115
Jon Skeet Avatar answered Sep 18 '22 17:09

Jon Skeet


java.time

I should like to contribute the modern answer. The SimpleDateFormat class is notoriously troublesome, and while it was reasonable to fight one’s way through with it when this question was asked six and a half years ago, today we have much better in java.time, the modern Java date and time API. SimpleDateFormat and its friend Date are now considered long outdated, so don’t use them anymore.

    DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("MM/uuuu");     String dateformat = "2012-11-17T00:00:00.000-05:00";     OffsetDateTime dateTime = OffsetDateTime.parse(dateformat);     String monthYear = dateTime.format(monthFormatter);     System.out.println(monthYear); 

Output:

11/2012

I am exploiting the fact that your string is in ISO 8601 format, the international standard, and that the classes of java.time parse this format as their default, that is, without any explicit formatter. It’s stil true what the other answers say, you need to parse the original string first, then format the resulting date-time object into a new string. Usually this requires two formatters, only in this case we’re lucky and can do with just one formatter.

What went wrong in your code

  • As others have said, SimpleDateFormat.format cannot accept a String argument, also when the parameter type is declared to be Object.
  • Because of the exception you didn’t get around to discovering: there is also a bug in your format pattern string, mm/yyyy. Lowercase mm os for minute of the hour. You need uppercase MM for month.
  • Finally the Java naming conventions say to use a lowercase first letter in variable names, so use lowercase m in monthYear (also because java.time includes a MonthYear class with uppercase M, so to avoid confusion).

Links

  • Oracle tutorial: Date Time explaining how to use java.time.
  • Wikipedia article: ISO 8601
like image 41
Ole V.V. Avatar answered Sep 21 '22 17:09

Ole V.V.