Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Date forms 
 

In this section we are demonstrating the retrieval of date from the Date object in various forms.

 

Date forms

                         

In this section we are demonstrating the retrieval of date from the Date object in various forms. In the following code segment we are performing various operations on Date object. We have explained, how to retrieve milliseconds from the date object, counting total number of milliseconds from a specific date, computes hash code for the date object, string representation of a date object and how to parse a Date object into its equalent string format.

Description of program:

In this example first we are creating a method Dateforms(), in which we are creating a Date class object date that contains a date of 1/1/1970. Now we are retrieving this date into milliseconds and taking it into a long type variable longmiliseconds, then computing a hash code by using hashCode() method for this date object, converting this date into its string equalent representation by using toString() method, parsing the date into string form by using parse() method and printing all these values on the console.

Here is the code of program:

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Dateforms {
    private static void Dateforms() {
    
        System.out.println("Retrieving miliseconds from a date object");
        
        // Establish a date object set in milliseconds relative to 1/1/1970 GMT
        Date date = new Date(1000L*60*60*24);

        // Retrieve the number of milliseconds since 1/1/1970 GMT (31536000000)
        long noofmiliseconds = date.getTime();
        System.out.println();
        System.out.println
            ("Total Number of milliseconds since 1/1/1970 (GMT) : " + noofmiliseconds);

        // Computes a hashcode for the date object (1471228935)
        int i = date.hashCode();
        System.out.println("Hash code for a Date object: " + i);

        // Retrieve the string representation of the date (Thu Dec 31 16:00:00 PST 1970)
        String strdate = date.toString();
        System.out.println("String representation of date is: " + strdate);
        System.out.println();

        System.out.println("Parsing of date into string form");
        Date newDate;
        String inputDate = "2005-06-27";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        System.out.print(inputDate + " parses as ");
        try {
            newDate = format.parse(inputDate);
            System.out.println(newDate);
        } catch (ParseException e) {
            System.out.println("Unparseable using " + format);
        }    
        System.out.println();
    }
    public static void main(String[] args) {
        System.out.println();
        Dateforms();
    }
}


Here is the output:

C:\Examples>java Dateforms

Retrieving miliseconds from a date object

Total Number of milliseconds since 1/1/1970 (GMT) : 86400000
Hash code for a Date object: 86400000
String representation of date is: Fri Jan 02 05:30:00 GMT+05:30 1970

Parsing of date into string form
2005-06-27 parses as Mon Jun 27 00:00:00 GMT+05:30 2005


Download of this program.

 

                         

» View all related tutorials
Related Tags: c file array class list ui lists method get name using this oo root example where to exam drive store

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.