Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java create date object using a value string

Tags:

java

date

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

like image 454
Marco Dinatsoli Avatar asked Apr 25 '13 06:04

Marco Dinatsoli


People also ask

How do you create a Date object in java?

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.

How do you pass a Date value in java?

SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy"); Date date = format. parse(request. getParameter("event_date")); Then you can convert java.

Can we convert Date to string?

We can convert Date to String in java using format() method of java. text. DateFormat class.


1 Answers

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.

like image 56
Freak Avatar answered Oct 21 '22 11:10

Freak