2

I am trying to convert date which is in string and got format of "yyyy-MM-dd HH:mm:ss" to "dd-MM-yyyy".

I have implmented following code but its giving : java.lang.IllegalArgumentException

        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date(values);
        String mydate = dateFormat.format(date);
Adil Bhatty
  • 17,190
  • 34
  • 81
  • 118
  • 1
    what line is throwing the exception? – hage Nov 29 '11 at 07:52
  • what is values.And are u sure simpleDateformatter is throwing exception – Abhishek bhutra Nov 29 '11 at 07:54
  • possible duplicate of [Java string to date conversion](http://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – Oleg Estekhin May 05 '14 at 06:17
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque May 11 '17 at 05:50

6 Answers6

7

First you have to parse the string representation of your date-time into a Date object.

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = (Date)formatter.parse("2011-11-29 12:34:25");

Then you format the Date object back into a String in your preferred format.

DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
String mydate = dateFormat.format(date);
Torben
  • 3,805
  • 26
  • 31
2

You need to parse the date, using another SimpleDateFormat

SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dateFormat2.parse(values);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String mydate = dateFormat.format(date);
SJuan76
  • 24,532
  • 6
  • 47
  • 87
1

tl;dr

LocalDateTime.parse( 
    "2011-11-29 12:34:25".replace( " " , "T " ) 
).format (
    DateTimeFormatter.ofPattern( "dd-MM-uuuu" )
)

29-11-2011

Using java.time

The modern approach uses the java.time classes rather than the troublesome legacy classes.

ISO 8601

Your input string of “2011-11-29 12:34:25” has a format nearly that of the ISO 8601 standard. To fully comply, replace the SPACE in the middle with a T.

String input = "2011-11-29 12:34:25".replace( " " , "T " );

Without any indication of time zone or offset-from-UTC, we parse as a LocalDateTime.

LocalDateTime ldt = LocalDateTime.parse( input ) ;

You want the date-only value, so extract a LocalDate. The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate ld = ldt.toLocalDate() ;

To generate a string in your desired format, you specify a custom formatting pattern.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MM-uuuu" );
String output = ld.format( f );

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

The IllegalArgumentException probably occurs when program trying to construct Date object

What's the value ?

Following code snippet runs correctly.

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
String mydate = dateFormat.format(date);

System.out.println(mydate);

Output :

29/11/2011

Rangi Lin
  • 9,303
  • 6
  • 45
  • 71
0

The constructor to Date takes a long representing the time in milliseconds. You need to use another SimpleDateFormat instance to parse your input string into a timestamp first.

rsp
  • 23,135
  • 6
  • 55
  • 69
0

try this way

String mydate = "2011-11-29 12:34:25"
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = null;

try{
   date = sdf.parse(mydate);
   SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
   String mydate = dateFormat.format(date);
}catch(Exception ex){
   // handle exception
}
Pratik
  • 30,639
  • 18
  • 84
  • 159