on 11-15-2006 7:55 AM
Hi,
How can I hard code a date (09/22/2006) in a date variable? Help me out.
Thank you.
Regards,
Dhana
hi Dhana,
Its simple.
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd-MMM-yyyy");
try {
java.util.Date dt = sdf.parse("15-APR-1978");
}catch(java.text.ParseException p) {
System.out.println(p.toString());
}
Or
String dat="01/03/2005";
long date=new SimpleDateFormat("MM/dd/yyyy").parse(dat,new ParsePosition(0)).getTime();
java.sql.Date dbDate=new java.sql.Date(date);
Hope this helps.
Regards,
Richa
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
it depends if your input is already parsed and how you want to set the time (hours, etc.).
Two sample ways :
String s = "09/22/2006";
SimpleDateFormat sd = new SimpleDateFormat("MM/dd/yyyy");
Date date1 = sd.parse(s);
Calendar c = Calendar.getInstance();
c.set(2006, 8, 22); //month is zero based
Date date2 = c.getTime();
Btw for basic java programming I would suggest searching for some java tutorials on the net and reading on your own instead of posting in forums. At least for me that is better way to learn.
HTH
Peter
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
24 | |
11 | |
9 | |
7 | |
6 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.