Home > How & Why, Interactive knowledge & Tips n Tricks & other reference stuff > How to handles dates in java without panic

How to handles dates in java without panic

July 29th, 2010 201 views Leave a comment Go to comments
java date4j

Handling dates, their formatting and other operations in java are little bit problematic. I never found any efficient tool to print time stamp or to operate dates like adding days or finding out last day of the month or whether current year is leap year etc.
date4j provides efficient solution to do all date related operations. This is a very good replacement of java.util.Date. Refer the below example for this;


import java.util.TimeZone;

import hirondelle.date4j.*;

public class date4jYamty {

	public static void main(String[] args) {
		//DateTime dt = new DateTime("");
		String dt = DateTime.now(TimeZone.getDefault()).format("YYYY-MM-DD-hh.mm.ss.ffffff");
		//DateTime dateAndTime = new DateTime("2010-01-19 23:59:59");
		//DateTime dateAndTime = new DateTime("2010-01-19 23:59:59.123456789");
		//DateTime dateOnly = new DateTime("2010-01-19");
		//DateTime timeOnly = new DateTime("23:59:59");
		//DateTime dateOnly = DateTime.forDateOnly(2010,01,19);
		//DateTime timeOnly = DateTime.forTimeOnly(23,59,59,0);		

		System.out.println(DateTime.now(TimeZone.getDefault())); //Y:2010 M:7 D:22 h:21 m:23 s:58 f:130000000
		System.out.println(dt); //2010-07-22-21.23.58.110000
		System.out.println(DateTime.today(TimeZone.getDefault())); //Y:2010 M:7 D:22 h:null m:null s:null f:null		

		//DateTime dt = new DateTime("2010-01-15 13:59:15");
		//boolean leap = dt.isLeapYear(); //false
		//dt.getNumDaysInMonth(); //31
		//dt.getStartOfMonth(); //2010-01-01, 00:00:00.000000000
		//dt.getEndOfDay(); //2010-01-15, 23:59:59.999999999
		//dt.format("YYYY-MM-DD"); //formats as '2010-01-15'
		//dt.plusDays(30); //30 days after Jan 15
		//dt.numDaysFrom(someDate); //returns an int
		//dueDate.lt(someDate); //less-than
		//dueDate.lteq(someDate); //less-than-or-equal-to

	}

}

Output :

Output for any statement is attached with statement itself as comment.
You can comment out rest statements. They are written for your knowledge.

Amit Gupta

Hey! this is Amit Gupta (amty). By profession, I am a Software Eng. And teaching is my passion. Sometimes I am a teacher, as you can see many technical tutorials on my site, sometimes I am a poet, And sometime just a friend of friends...

  1. August 1st, 2010 at 09:22 | #1

    Hi luchosar
    I suppose u put a wrong question in wrong article comments. Is it for amty thumb post?

  2. August 1st, 2010 at 04:41 | #2

    Hello greetings.
    Two questions:
    1 – I put the plugin in the home but only in a specific category, which I use code
    2 – I put the post in horizontal
    Thanks

    ————-
    Hola saludos.
    Dos preguntas:
    1-Deseo colocar el plugin en el home pero solo de una categoria especifica, que codigo debo usar
    2-Deseo colocar los post en horizontal
    Gracias

  3. July 29th, 2010 at 14:54 | #3

    thnx

  4. gurub_n
    July 29th, 2010 at 07:23 | #4

    Good work buddy..