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.
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
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);
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.
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);
LocalDateTime.parse(
"2011-11-29 12:34:25".replace( " " , "T " )
).format (
DateTimeFormatter.ofPattern( "dd-MM-uuuu" )
)
29-11-2011
The modern approach uses the java.time classes rather than the troublesome legacy classes.
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 );
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.
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
}