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
You can create a Date object using the Date() constructor of java. util. Date constructor as shown in the following example. The object created using this constructor represents the current time.
SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy"); Date date = format. parse(request. getParameter("event_date")); Then you can convert java.
We can convert Date to String in java using format() method of java. text. DateFormat class.
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
.
refer the official doc for more Date and Time Patterns used in SimpleDateFormat options.
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