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. 

Currently rated 5.0 by 1 people

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

Tags:

Java

Related posts

Comments

6/11/2010 2:50:17 PM

pingback

Pingback from condosencinitas.com

car insurance cheap quote

condosencinitas.com

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

<<  May 2012  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

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 2012

      Sign in