how to strip time component from date variable in Java

When I pass this parameter, "04202010" (April 20, 2010), my screen works fine.

Upon startup, there is no parameter, and the screen comes up blank.

I have logic to use the current date on startup, but it has the time component and I believe this is why startup doesn't work.

so from the code below, if the date is not null (e.g. 04202010), then report date shows no time component.

if the date is null, then reportDate = new Date() results in the time component being present.
- how can I strip the time component in the else section ?

(off to bed now)


if (date != null) {
SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy");
reportDate = sdf.parse(date);
} else {
reportDate = new Date();
}

Open in new window

LVL 1
Alaska CowboyAsked:
Who is Participating?
 
ManishConnect With a Mentor LeadCommented:
Check this if you need to adjust anything in format.
http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html
0
 
Gurvinder Pal SinghCommented:
try this

Date today = new Date();
new java.sql.Timestamp(today.getTime());

0
 
a_bCommented:
Change the logic as shown below -

if (date== null)
 { date - new Date();}
SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy");
reportDate = sdf.parse(date);


0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Pramod KumarConnect With a Mentor Commented:
I don't how time part in the date causing problem in your program but anyway here is the code by which the return the date without time part.

        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy");
            reportDate = sdf.parse(date);
        } else {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.HOUR,0);
            c.set(Calendar.MINUTE,0);
            c.set(Calendar.SECOND,0);
            c.set(Calendar.MILLISECOND,0);

            reportDate = c.getTime();
        }

Open in new window

0
 
ManishLeadCommented:
          } else{    
             reportDate = new Date();
             Calendar cal = Calendar.getInstance();
             cal.setTime(reportDate);
             cal.set(Calendar.HOUR_OF_DAY, 0);
             cal.set(Calendar.MINUTE, 0);
             cal.set(Calendar.SECOND, 0);
             
             reportDate= cal.getTime();

}
0
 
ManishLeadCommented:
sorry, I didnt see previous comments.
0
 
Alaska CowboyAuthor Commented:
thanks all for the late night (to me) tips . . .
I wasn't as clear as I should have been, I want to REMOVE the time component of sysdate and be left with 04282010.
so here's my refined request:
- to get a string from sysdate in the formart mmddyyyy
- pass that string to SimpleDateFormat
a_b, "date" is a string that's passed into SimpleDateFormat, see full snippet below
 
 

		String date = request.getParameter("DATE");
		Date reportDate = null;
	
		if (date != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy");
			reportDate = sdf.parse(date);
		} else {
			reportDate = new Date();
		}

Open in new window

0
 
a_bConnect With a Mentor Commented:
if (date== null)
 { Date d = new Date();date = d.toString();}
SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy");
reportDate = sdf.parse(date);
0
 
Alaska CowboyAuthor Commented:
a_b, thanks.
but I need to parse out mm + dd + yyyy
I tried it and got this error message:
Error 500: Request processing failed; nested exception is java.text.ParseException: Unparseable date: "Wed Apr 28 09:13:03 EDT 2010"  
something like this:
if (date== null) {
  Date d = new Date();
  date = d.toString("mon") || d.toString("dd") || d.toString("yyyy");
}
0
 
ManishConnect With a Mentor LeadCommented:
So you are getting
 String date ="Wed Apr 28 09:13:03 EDT 2010";
then use
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");

then parse date string, you will gate Date object.

0
 
Alaska CowboyAuthor Commented:
karanw, ok, thank you, I will check later this afternoon.
0
 
Alaska CowboyAuthor Commented:
ok, I fixed it by truncating the time component in Oracle, trunc(:date), that did the trick.
but the tips on managing time / strings in Java were very helpful, thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.