The Artima Developer Community
Sponsored Link

Java Answers Forum
how get todays Date with format in Date Object

3 replies on 1 page. Most recent reply: Mar 8, 2002 10:19 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Lizeth

Posts: 5
Nickname: liz
Registered: Mar, 2002

how get todays Date with format in Date Object Posted: Mar 6, 2002 1:11 PM
Reply to this message Reply
Advertisement
Hi,
I need help!
I need to insert a the Database todays Date in the format "MM-dd-yyyy". The datatype in the dataBase is Date, then I need a Date Object with the correct format.

I already tried with SimpleDateFormat, the problem is the method Format always return a String, then from the String how I can convert to Date with the same format.

Please help me!
Thanks in advance.
Lizeth


Anil S.

Posts: 13
Nickname: anil
Registered: Feb, 2002

Re: how get todays Date with format in Date Object Posted: Mar 7, 2002 3:15 PM
Reply to this message Reply
Take a look at the API docs for SimpleDateFormat,
you can make use of the parse() method to return an object of type Date, also look at the parse method signature of DateFormat extended by SimpleDateFormat,
you could make use of parse( String date )

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: how get todays Date with format in Date Object Posted: Mar 8, 2002 10:11 AM
Reply to this message Reply
You can create your own version of Today's date in the format "MM-DD-YYYY" as in the following method:

<java>
/** Returns Today's s Date as String in in the format "MM-dd-yyyy".
*/
public String getTodaysDate(){
Calendar today = Calendar.getInstance();
String month = String.valueOf(today.get(Calendar.MONTH) + 1);
if (month.length() == 1) month = "0" + month;
String dayofmonth = String.valueOf(today.get(Calendar.DAY_OF_MONTH));
if (dayofmonth.length() == 1) dayofmonth = "0" + dayofmonth ;
String year = String.valueOf(today.get(Calendar.YEAR));
return (month + "-" + dayofmonth + "-" + year);
}
</java>

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: how get todays Date with format in Date Object Posted: Mar 8, 2002 10:19 AM
Reply to this message Reply
You can create a java.sql.Date object with a method such as:
<java>
/** Returns Today's s Date as java.sql.Date.
*/
public java.sql.Date getTodaysDate(){
Calendar today = Calendar.getInstance();
String month = String.valueOf(today.get(Calendar.MONTH) + 1);
if (month.length() == 1) month = "0" + month;
String dayofmonth = String.valueOf(today.get(Calendar.DAY_OF_MONTH));
if (dayofmonth.length() == 1) dayofmonth = "0" + dayofmonth ;
String year = String.valueOf(today.get(Calendar.YEAR));
return java.sql.Date.valueOf(year+ "-" + dayofmonth + "-" + month);
}
</java>

Flat View: This topic has 3 replies on 1 page
Topic: conncect to database/JDBC Previous Topic   Next Topic Topic: reading text file through applet

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use