Reading:  

Quick introduction to Java Programming language - Part 1


Working with Date and Time

The Java.util package contains Date class which encapsulates current time and date.There are two constructors for date class, one initialize current date time of system and other accepts long millisecond from midnight January 1, 1970.

Date( )
Date(long millisec)

Some of the date object’s methods are:

Methods with Description

long getTime( )

Returns number of milliseconds elapsed since January 1, 1970

boolean after(Date date)

Returns true if invoking Date object contains a date which is after than one specified by date.

boolean before(Date date)

Returns true if invoking Date object contains a date which is before than one specified by date.

void setTime(long time)

Sets time and date as specified by time.

String toString( )

Converts invoking Date object to string and returns result.

 

Getting Current Date & Time in Java

import java.util.Date;
  public class DateExample {
   public static void main(String args[]) {
         Date newdate = new Date();
		 System.out.println(newdate.toString());
   }
}

Output : Sat Feb 21 09:45:34 CDT 2015

 

Above code returns the current system date and display it in string format.

Date Formatting using SimpleDateFormat:

public class DateExample {
   public static void main(String args[]) {
		Date dtNow = new Date( );
		SimpleDateFormatdtfrmt = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
		System.out.println(dtfrmt .format(dtNow));
   }
}


Output : Sun 2015.02.21 at 04:15:01 PM PDT

The SimpleDateFormat class is used for parsing and formatting dates .

Description

This tutorial is targetted for beginers seeking a quick get on with guide on Java programming language. 3 parts include 

  1. Java basics
  2. Object oriented programming
  3. Advance concepts

Each part is subdivided in multiple chapters.

Java basics has the following chapters

  1. Introduction
  2. Environment Setup
  3. Basic Syntax
  4. Objects and Classes
  5. Basic Data Types
  6. Variable Types
  7. Modifier Types
  8. Basic Operators
  9. Loops
  10. Decision Making
  11. Numbers Class
  12. Character and String Class
  13. Arrays
  14. Date and Time
  15. Regular Expression
  16. Methods
  17. Streams, Files and I/O
  18. Exceptions Handling

Thanks for reading and as always, your feedback is very important to us. Let us know how we can improve and if you found any issues with this write up, send us a correction.



Audience

Beginners or students seeking a refresher on Java language

Author: Subject Coach
Added on: 9th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!