Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to split date and time?

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
DateFormat.getDateInstance();
DateFormat.getTimeInstance();
 
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
    Bookmark Topic Watch Topic
  • New Topic