Working with Dates in Java

by lichen 7/11/2007 3:52:00 PM

For .net programmers, we used to work in a single System.DateTime structure. In Java, unfortunately, we often need to work with several classes:

  • java.util.Date

java.util.Date internally store the date/time as number of milliseconds after Jan. 1, 1970 using long. There are 86,400,000 millilseconds a day. The default constructor represents the date/time that the object is constructed. The main advantage of using Date class is that it is possible to parse a String to Date or format a Date to String using DateFormat class.

  • java.text.DateFormat, java.text.SimpleDateFormat

These two classes can convert a String to/from to Date. One can use one of the overloaded DateFormat.getDateInstance() methods to get a localized format. For custom format, it is better to create an instance SimpleDateFormat using the format string in the constructor.

  • java.util.GregorianCalendar

We need this class in order to work with parts of a date or construct a date from parts. It also allow us to add a range to a date. We can convert between Date and GregorianCalendar by using the setTime() and getTime() method of teh GregorianCalendar class.

There is a nice article on Javaworld that contains many useful examples. 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Java

Related posts

Comments

Comments are closed

Powered by BlogEngine.NET 1.2.0.0
Theme by Mads Kristensen

About the author

Name of author Author name
Something about me and what I do.

E-mail me Send mail

Calendar

<<  February 2010  >>
MoTuWeThFrSaSu
25262728293031
1234567
891011121314
15161718192021
22232425262728
1234567

View posts in large calendar

Pages

    Recent comments

    Authors

    Tags

      Disclaimer

      The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

      © Copyright 2010

      Sign in