Date value

Hi all,

       I need to create a new date with a value of april 20th 2004. How do i do that....  new Date(int,int,int) is depricated, so how can i do this

Thank you
letsbedecentAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
I think i've covered this in your other question. There are two ways:

1. Use a Calendar
2. Use a DateFormat

Here's 2:

DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date d = df.parse("20/04/2004");

(You need to catch exceptions)


0
 
bloodredsunConnect With a Mentor Commented:
   Calendar myDate= new GregorianCalendar(2004, Calendar.APRIL, 25);
    Date date = myDate.getTime();
0
 
letsbedecentAuthor Commented:
What all should be imported to get this working ?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
bloodredsunCommented:
Oops, change the 25 to 20, but I'm sure you get the idea!
0
 
bloodredsunCommented:
imports are:

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
0
 
bloodredsunCommented:
or just do:
import java.util.*;
0
 
bloodredsunCommented:
Here's a working example:

--------------
package examples;

import java.util.*;

public class MyDate {
      
      public static void main(String[] args){
            Calendar mydate= new GregorianCalendar(2004, Calendar.APRIL, 20);
            Date date = mydate.getTime();
            System.out.println( "Date is " + date )      ;
      }
}
--------------
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.