Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date format~pls help

Status
Not open for further replies.

ttea

IS-IT--Management
Feb 24, 2003
11
HK
how to display the current date in format yyyy-MM-DD, same as the default format of mySQL

i try this in jsp,
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = df.format(theDate);
out.print(formattedDate);

error
cannot resolve symbol symbol : class DateFormat
 
Change Date to your desired format and then store as string and then use this string to put into query. The code for changing date into yyyy-mm-dd is:

<%

java.util.Date date = new java.util.Date();

int mm = date.getMonth()+1;
int yyyy = date.getYear()+1900;
int dd = date.getDate();
String myDate = yyyy + &quot;-&quot; + mm + &quot;-&quot; + dd;

out.println(myDate);

%>
 
Did you import java.text.SimpleDateFormat and java.text.DateFormat? If you use both (I'm not sure you should declare df as DateFormat, because SimpleDateFormat is its subclass), you should import both. I also suppose that your approach is much better than provided by ak44khatri.


Regards, Dima
 
Well my approach is very basic one as I have never used dateformart stuff.

I have not started using many classes from API and therefore always find a tricky (and less professional) approach to do it. Sometimes I end up making a class which I later realize that it already available. For example to sort an array of strings, I developed my our code but later came to know that Arrays.sort(); was available to do it.

Ajeet
 
java.sql.Date today = new java.sql.Date(System.currentTimeMillis());
SimpleDateFormat df = new SimpleDateFormat(&quot;MMM-dd-yyyy&quot;);
String strToday = df.format(today);
 
Do you know how you can subtract days from a particular date? Say you wanted to go back 30 days?

Thanks.



Change Your Thinking, Change Your Life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top