[java] 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

This question is related to java date

The answer is


Try this :-

try{
        String valuee="25/04/2013";
        Date currentDate =new SimpleDateFormat("dd/MM/yyyy").parse(valuee);
        System.out.println("Date is ::"+currentDate);
        }catch(Exception e){
            System.out.println("Error::"+e);
            e.printStackTrace();
        }

Output:-

   Date is ::Thu Apr 25 00:00:00 GMT+05:30 2013

Your value should be proper format.

In your question also you have asked for this below :-

   Date currentDate = new Date(value);

This style of date constructor is already deprecated.So, its no more use.Being we know that Date has 6 constructor.Read more


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


try this, it worked for me.

 String inputString = "01-01-1900";
    Date inputDate= null;
    try {
        inputDate = new SimpleDateFormat("dd-MM-yyyy").parse(inputString);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    dp.getDatePicker().setMinDate(inputDate.getTime());

 import java.util.Date;
 import java.text.SimpleDateFormat;

Above is the import method, below is the simple code for Date

 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
 Date date = new Date();

 system.out.println((dateFormat.format(date))); 

Use SimpleDateFormat parse method:

import java.text.DateFormat;
import java.text.SimpleDateFormat;

String inputString = "11-11-2012";
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date inputDate = dateFormat.parse(inputString, dateFormat );

Since we have Java 8 with LocalDate I would suggest use next:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

String inputString = "11-11-2012";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate inputDate = LocalDate.parse(inputString,formatter);

Here is the optimized solution to do it with SimpleDateFormat parse() method.

SimpleDateFormat formatter = new SimpleDateFormat(
        "EEEE, dd/MM/yyyy/hh:mm:ss");
String strDate = formatter.format(new Date());

try {
    Date pDate = formatter.parse(strDate);
} catch (ParseException e) { // note: parse method can throw ParseException
    e.printStackTrace();
}

Few things to notice

  • We don't need to create a Calendar instance to get the current date & time instead use new Date()
  • Also it doesn't require 2 instances of SimpleDateFormat as found in the most voted answer for this question. It's just a waste of memory
  • Furthermore, catching a generic exception like Exception is a bad practice when we know that the parse method only stands a chance to throw a ParseException. We need to be as specific as possible when dealing with Exceptions. You can refer, throws Exception bad practice?

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:-

Calendar cal = Calendar.getInstance();
Date date = cal.getTime();

The reason being, the String which you're printing is just a String representation of the Date 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 a String and trying to get back a Date from it - makes no sense. Date has no format as such. You can only get a String representation of that using the SDF.