• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to split date and time?

 
Mindy Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DateFormat.getDateInstance();
DateFormat.getTimeInstance();
 
Barry Andrews
Ranch Hand
Posts: 523
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
Barry

[This message has been edited by Barry Andrews (edited July 18, 2001).]
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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));

Bosun
 
Mindy Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
Mindy
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic