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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Pramod KumarCommented:
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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_bCommented:
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
ManishLeadCommented:
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
ManishLeadCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.