Marco Dinatsoli Marco Dinatsoli - 2 months ago 13
Java Question

java create date object using a value string

I am using this to get the current time :

java.util.Calendar cal = java.util.Calendar.getInstance();
System.out.println(new java.text.SimpleDateFormat("EEEE, dd/MM/yyyy/hh:mm:ss")
.format(cal.getTime()));


I want to put the value (which I print it) into a date object, I tried this:

Date currentDate = new Date(value);


but eclipse tells me that this function is not good.

Edit
the
value
is the value that I printed to you using system.out.println

Answer

Whenever you want to convert a String to Date object then use SimpleDateFormat#parse
Try to use

String dateInString = new java.text.SimpleDateFormat("EEEE, dd/MM/yyyy/hh:mm:ss")
        .format(cal.getTime())
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, dd/MM/yyyy/hh:mm:ss");
Date parsedDate = formatter.parse(dateInString);

.Additional thing is if you want to convert a Date to String then you should use SimpleDateFormat#format function.
Now the Point for you is new Date(String) is deprecated and not recommended now.Now whenever anyone wants to parse , then he/she should use SimpleDateFormat#parse.

Comments