Home | Login | Blog | Forum | Download | RSS Feed | Ajax | Hello-World | J2EE | BPM | EJB | FileNet | Hibernate | ORM | IBM | ILOG | Interview | FAQs |

Java | Apache | JVM | Others | BRMS | Oracle | Pega | SOA | Sun-Certification | Web-Service | websphere | XML |
 

JAVA DEVELOPER SITE - COMPLETE JAVA TUTORIALS

Java Date API & Date Format


Share
 
 

Useful Java Links


       


The Date class is used to represent a Date and time. We can manipulate  the day, month ,year, day-of-the-week, hours, minutes, and seconds. There are several constructors for Date objects. The simple one, Date (), initializes the object with the current date and time. Most of this class methods have been deprecated . Its API design didnt do a good job of handling internationalization and localization situations. The Date class is used as temporary bridge to format a Calender object using the DateFormat class. Three more constructors offer increasing level of specificity  for how accurate we would like the time to be.

 

ü  Date(year,month,date)will set the time to 00:00:00(midnight) on the specified day.

ü  Date(year, month,date,hours,minutes)will set the date and time,leaving the seconds set to 0.

ü  Date(year,month,date,hours, minutes,seconds) will exactly specify the time.

 

            The  last constructor for Date will parse  a String representation of the date and time into an object. Date contains a static method called parse which can be called directly to parse a String formated date directly into the  long representation; parse is used by this constructor. Date parses many formats of String dates.

 

            Date includes set of methods for getting and setting the year,month,day hour, minute and seconds of an instance. You can only get the day-of-the-week, since it is bound to the day of the month. Each of the get functions -getYear, getMonth, getDate, getDay, getHours, getMinutes, and getSeconds-returns an int. Each of the set methods-setYear, setMonth, setDate, setHours, setMinute, and setSeconds-take an int parameter. We can get the long representation of  Date object with the getTime method. This the number of milliseconds since January 1,1970. We can reconstruct a Date Object some time later by passing in this long to the Date constructor, or we can change a Date object by calling setTime with a new long.

 

            If we have two Date objects and we want to compare , we could convert them to their long representation to compare them as integer numbers of milliseconds. The Date class includes three methods that can be used for direct comparisonbefore,after, and equals. For example new Date(96,2,18).before(new Date(96,2,12)) returns true, because the 12th came before the 18th. Here are the three method signatures:

ü  boolean before(Date when)

ü  boolean after(Date when)

ü  boolean equal(Date other)

 

            Date objects can be converted to String representations in  several forms. First, the normal toString method converts Date object to a String that looks like this : “ Thu Feb 15 22:42:04 2011”. Then, the toLocaleString method converts Date to a shorter String like this: 02/15/11 22:42:04”. Last, toGMTString converts a Date to the Greenwich Mean Time format like this:”16 Feb 2011 06:42:04 GMT”. To determine how far from GMT your local time zone is, use getTimezoneOffset, which returns the offset in minutes.

 

            Sample program:-

            class Date {
                         int dd, mm, yy;
                         public void initDate(){
                         dd = mm = yy = 0;
             }
             public void setDate(int d, int m, int y){
                         dd = d;
                        mm = m;
                        yy = y;
         




Download Latest Java/J2EE eBooks, SCJP Dumps, SCWCD Dumps, Pega Tutorial, IBM WebSphere BPM tutorials and many more


If you need any urgent assistance on Java_Date_Format_Pattern_Tutorial, kindly email your requirement to us at : info@javagenious.com or Contact-an-Expert. Our experts will try their best to solve your problem.

You can also subscribe to our newsletters on Java_Date_Format_Pattern_Tutorial, to receive updates on Java_Date_Format_Pattern_Tutorial,via email.Enter you email below:

Enter your email address:

Delivered by FeedBurner



Keyword Tags: Java_Date_Format_Pattern_Tutorial tutorial,Java_Date_Format_Pattern_Tutorial in java,Concepts of Java_Date_Format_Pattern_Tutorial,Java,J2EE,Interview Questions,Java_Date_Format_Pattern_Tutorial Examples


Comments:


Post Your Comment:

*

Yes, I would like to recieve email notifications on my reply.

Popular Posts:

 
Popular Downloads

SCJP 1.6 Dumps
JSP Interview Questions
Struts Interview Questions
Hibernate Interview Questions
SCWCD Dumps
SCBCD Dumps

Java/J2EE Tutorial
JMS Tutorials
seam richfaces tutorial
richfaces live demo
jquery example demo
JQuery Tutorial and Source Code
JSF Tutorial
XSL Tutorial
J2EE JMS
CORBA Applications
Java CERTIFICATION
SCJP 1.5
SCJP 1.6
SCWCD 1.5
SCBCD
Mis
Ajax autosuggest autocomplete from database
Struts forms and validation with webflow
Java Apache POI Examples
JDBC Tutorial
Oracle 9i & 10g
MYSQL Tutorial
php Tutorial
JQuery Examples
Upload Java File
JAXB Examples
PL-SQL Tutorial
Dojo AJAX
EJB Examples
Java IDE
Eclipse
NetBeans
IBM Rational Weblogic Workshop
Downloads
SCJP 1.6 Dumps
SCJP 1.5 DUMP
SCJP Material
SCWCD
Java Security
SCBCD Dumps
External Links
SUN Java
JOracle
IBM
PHP Certification
Technical Interview Questions
Amazon Interview Questions
Google Interview Questions
Microsoft Interview Questions
IBM Interview Questions
Yahoo Interview Questions
All Tutorials
php mysql tutorial
SCJP 1.5/1.6
SCWCD 1.5/1.6
Polymorphism
Thread Tutorial
Generics & Exceptions
Spring Struts Hibernate
Java Collections
MAP
SET
LIST
VECTOR
Advanced Java
Stateless Session Bean
Introduction to EJB
Stateful Session Bean
Java Design Patterns
HOME | FORUM | ABOUT | BLOGS | CONTACT-US