Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Date forms

 
 

Share on Google+Share on Google+

Date forms

Advertisement
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.

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 19, 2008

Related Tutorials

Discuss: Date forms  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com