Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes how to split  date and time? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to split  date and time?" Watch "how to split  date and time?" New topic

how to split date and time?

Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
Hi All,
I am wondering if there is any method allow you to get hour, minutes, second, year, month and date from any format of datetime?
Here is an example to get the date and time, is there any method to split them?
Date now = new Date();
DateFormat formatter =
DateFormat.getDateTimeInstance( DateFormat.LONG, DateFormat.MEDIUM);
strStartDateTime = formatter.format(now);
System.out.println("Right now is: " + strStartDateTime);
The output is like that: July 18, 2001 11:22:26 AM
Thank you!
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521

"JavaRanch, where the deer and the Certified play" - David O'Meara
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Check out the GregorianCalendar class. You can get whatever you want. Here is an example for you:
import java.util.*;

Alternately, if you really want to use the Date class, you could create your on methods for getting the hour, min, sec, etc., by parsing the string to get these values, but if you are in a hurry I would just use this. Hope this helps!

[This message has been edited by Barry Andrews (edited July 18, 2001).]
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Here is an example using the SimpleDateFormat class

Date today = new Date();
DateFormat format = new SimpleDateFormat("MM/dd/yyyy");
DateFormat year = new SimpleDateFormat("yyyy");
DateFormat month = new SimpleDateFormat("MM");
DateFormat day = new SimpleDateFormat("dd");
System.out.println("today is: " + format.format(today));
System.out.println("The year is: " + year.format(today));
System.out.println("The month is: " + month.format(today));
System.out.println("The day is: " + day.format(today));


So much trouble in the world -- Bob Marley
Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
Thanks all! I did it. I tried all of the methods you provided and finally, i created a class and some methods to split the date and time.
Thanks so much! Thanks!
I agree. Here's the link:
subject: how to split date and time?
It's not a secret anymore!