aspose file tools*
The moose likes Java in General and the fly likes how do i get todays dat without the time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how do i get todays dat without the time" Watch "how do i get todays dat without the time" New topic
Author

how do i get todays dat without the time

Robert Johnson
Ranch Hand

Joined: Feb 11, 2005
Posts: 32
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?


Whats in a name?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410



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 ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ct Arrington
Author
Greenhorn

Joined: Jan 17, 2001
Posts: 27
Hi Graham

Another alternative is to use the SimpleDateFormat class.

SimpleDateFormat Javadoc

In your case, it would look like:




Hope it helps
CT


Blog | Getting Started in Software Development
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


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 ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Vijay Kumar
Ranch Hand

Joined: Jul 24, 2003
Posts: 260
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

Joined: Feb 11, 2005
Posts: 32
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

Joined: Feb 11, 2005
Posts: 32
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

Joined: Jan 10, 2002
Posts: 61658
    
  67

Not much to do with JSP, so off to the Java in General(intermediate) forum...


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how do i get todays dat without the time