Quick introduction to Java Programming language - Part 1
Chapters
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
- Java basics
- Object oriented programming
- Advance concepts
Each part is subdivided in multiple chapters.
Java basics has the following chapters
- Introduction
- Environment Setup
- Basic Syntax
- Objects and Classes
- Basic Data Types
- Variable Types
- Modifier Types
- Basic Operators
- Loops
- Decision Making
- Numbers Class
- Character and String Class
- Arrays
- Date and Time
- Regular Expression
- Methods
- Streams, Files and I/O
- 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!