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); } }
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.
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.
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.
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.
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.
SimpleDateFormat.format
cannot accept a String
argument, also when the parameter type is declared to be Object
.mm/yyyy
. Lowercase mm
os for minute of the hour. You need uppercase MM
for month.m
in monthYear
(also because java.time includes a MonthYear
class with uppercase M
, so to avoid confusion).java.time
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With