27

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

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253

9 Answers9

60

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.

ArigatoManga
  • 594
  • 5
  • 23
Freak
  • 6,786
  • 5
  • 36
  • 54
  • It is your date in String format.You need to set format in in `new SimpleDateFormat(format)` exactly as your string have and then just need to parse it.As your `new java.text.SimpleDateFormat("EEEE, dd/MM/yyyy/hh:mm:ss") .format(cald.getTime())` will return a String and in question I guess you want to convert it into `Date` then this is the way that I answered – Freak Apr 25 '13 at 06:56
  • what is dateInString?? i didn't parse anything, – Marco Dinatsoli Apr 25 '13 at 06:57
  • +1 `dateInString` is the string you have which you want to turn into a date. – Peter Lawrey Apr 25 '13 at 06:58
  • @MarcoDinatsoli please see the updated answer.I hope now you would understand what is `dateInString` – Freak Apr 25 '13 at 07:00
  • the resutls for ur code is `Thu Apr 25 00:23:26 PDT 2013` and it is not in the format which i want , why please? – Marco Dinatsoli Apr 25 '13 at 07:25
  • because now you are printing `java.util.Date` and you cannot override its format by using `java.util.Date`.If you want to print your desired format then you have to `format` your date otherwise with using `Date` object , you can't do this.Simply you can say that The Date class does not store any information about formatting. The Date class exist basically merely of a private long milliseconds field – Freak Apr 25 '13 at 07:30
36

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);
Varun
  • 4,342
  • 19
  • 84
  • 119
alexey28
  • 5,170
  • 1
  • 20
  • 25
3

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

Community
  • 1
  • 1
Nikhil Agrawal
  • 26,128
  • 21
  • 90
  • 126
3
 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))); 
akki0996
  • 713
  • 2
  • 6
  • 14
1

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

JDGuide
  • 6,239
  • 12
  • 46
  • 64
1

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?
Community
  • 1
  • 1
Hamzeen Hameem
  • 2,360
  • 1
  • 27
  • 28
1

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());
manasera
  • 61
  • 4
0

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.

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

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.

Rahul
  • 44,383
  • 11
  • 84
  • 103