• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how do i get todays dat without the time

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it doesnt matter if its in a jsp page or a servlet. its all the same.

heres two ways that i know of getting the date but the time and timezone comes with it.
Date date = new Date();
java.util.Date today = new java.util.Date();

it outputs the date like this...
Fri Feb 11 11:58:17 GMT 2005

i want the date in the format of 11-Feb-2005 because i need to use them with an oracle database.

is there any way i can get it like that?

i might be able to strip out just the fields i want in the above format and then use some sort of toString() method to format them the way i need. but i dont know how to do that.

any ideas?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


http://java.sun.com/j2se/1.4.2/docs/api/java/util/GregorianCalendar.html

And there's also:
http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Date.html
[ February 11, 2005: Message edited by: Ben Souther ]
 
Author
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Graham

Another alternative is to use the SimpleDateFormat class.

SimpleDateFormat Javadoc

In your case, it would look like:




Hope it helps
CT
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i want the date in the format of 11-Feb-2005 because i need to use them with an oracle database.


The format your date is displayed in does not matter if you use a PreparedStatement and bind your query parameters accordingly.
[ February 11, 2005: Message edited by: Paul Sturrock ]
 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Paul
this is way I used to compare with oracle

it'll work
..........
..........
// SET THE FORMAT
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

// MAKE A STRING
String strDt=request.getParameter("DD")+"/"+request.getParameter("MM")+"/"+request.getParameter("YY");

// MAKE UTIL DATE
java.util.Date utilDate = formatter.parse(strDt);

try{utilDate = formatter.parse(utilDate);}catch(ParseException p){}

// CONVERT UTIL DATE TO SQL DATE
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

String qry="SELECT NAME FROM FRIEND LIST WHERE DOB=?";
pstmt=conn.prepareStatement(qry);
pstmt.setDate(1,sqlDate);
ResultSet rs=pstmt.executeQuery();

..........
..........

Vijay
 
Robert Johnson
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got it working, this is the way i did it...

<%!
public String getToDaysDate(String s)
{
String months[]={"Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec"
};
Calendar cal=Calendar.getInstance();
if (s=="/")
return cal.get(Calendar.DATE)+"-"+months[cal.get(Calendar.MONTH)]+"-"+cal.get(cal.YEAR);
else
return (cal.get(Calendar.MONTH)+1)+"/"+cal.get(Calendar.DATE)+"/"+cal.get(cal.YEAR);

}
String today1=getToDaysDate("/");
out.println("<br>date is "+today1);
%>




but how do you check to see if one date is either in the future or the past.


for example an application form will have two dates (stored in the database). a start date which tells the jsp page to display the application form and an end date which tells the jsp page not to display it anymore.

so if i have todays date 11-Feb-2005 and the end date (for example 12-Feb-2005), how can i check to see if the date is in the past or future.

the play date and end date will not be the same date so i cant just compare the values. the dates are stored in Strings so maybe i will have to convert them to some other date format?

any ideas?
 
Robert Johnson
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is a way of doing it but i have to convert my String to the GregorianCalendar format( eg, from '11-Feb-2005' to
'2005, Calender.FEBRUARY, 11'


Calendar xmas = new GregorianCalendar(1998, Calendar.DECEMBER, 25);

Calendar newyears = new GregorianCalendar(1999, Calendar.JANUARY, 1);

// Determine which is earlier
boolean b = xmas.after(newyears); // false
b = xmas.before(newyears); // true


// Get difference in milliseconds
long diffMillis = newyears.getTimeInMillis()-xmas.getTimeInMillis();

// Get difference in seconds
long diffSecs = diffMillis/(1000); // 604800

// Get difference in minutes
long diffMins = diffMillis/(60*1000); // 10080

// Get difference in hours
long diffHours = diffMillis/(60*60*1000); // 168

// Get difference in days
long diffDays = diffMillis/(24*60*60*1000); // 7
 
Sheriff
Posts: 67745
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not much to do with JSP, so off to the Java in General(intermediate) forum...
 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic