• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how do i get todays dat without the time

 
Robert Johnson
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ct Arrington
Author
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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 ]
 
Vijay Kumar
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not much to do with JSP, so off to the Java in General(intermediate) forum...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic