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
FIRST OF ALL KNOW THE REASON WHY ECLIPSE IS DOING SO.
Date has only one constructor Date(long date) which asks for date in long data type.
The constructor you are using
Date(String s) Deprecated. As of JDK version 1.1, replaced by DateFormat.parse(String s).
Thats why eclipse tells that this function is not good.
See this official docs
http://docs.oracle.com/javase/6/docs/api/java/util/Date.html
Deprecated methods from your context -- Source -- http://www.coderanch.com/t/378728/java/java/Deprecated-methods
There are a number of reasons why a method or class may become deprecated. An API may not be easily extensible without breaking backwards compatibility, and thus be superseded by a more powerful API (e.g., java.util.Date has been deprecated in favor of Calendar, or the Java 1.0 event model). It may also simply not work or produce incorrect results under certain circumstances (e.g., some of the java.io stream classes do not work properly with some encodings). Sometimes an API is just ill-conceived (SingleThreadModel in the servlet API), and gets replaced by nothing. And some of the early calls have been replaced by "Java Bean"-compatible methods (size by getSize, bounds by getBounds etc.)
SEVRAL SOLUTIONS ARE THERE JUST GOOGLE IT--
You can use date(long date) By converting your date String into long milliseconds and stackoverflow has so many post for that purpose.
converting a date string into milliseconds in java
Whenever you want to convert a String to Date object then use
SimpleDateFormat#parse
Try to use
.Additional thing is if you want to convert a
Date
toString
then you should useSimpleDateFormat#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 useSimpleDateFormat#parse
.refer the official doc for more Date and Time Patterns used in SimpleDateFormat options.
try this, it worked for me.
It is because value coming String (Java Date object constructor for getting string is deprecated)
and
Date(String)
is deprecated.Have a look at jodatime or you could put @SuppressWarnings({“deprecation”}) outside the method calling the
Date(String)
constructor.What you're basically trying to do is this:-
The reason being, the
String
which you're printing is just aString
representation of theDate
in your required format. If you try to convert it to date, you'll eventually end up doing what I've mentioned above.Formatting
Date
(cal.getTime()) to aString
and trying to get back aDate
from it - makes no sense.Date
has no format as such. You can only get aString
representation of that using the SDF.Here is the optimized solution to do it with
SimpleDateFormat
parse()
method.Few things to notice
new Date()
SimpleDateFormat
as found in the most voted answer for this question. It's just a waste of memoryException
is a bad practice when we know that theparse
method only stands a chance to throw aParseException
. We need to be as specific as possible when dealing with Exceptions. You can refer, throws Exception bad practice?