HANDLING DATES IN JAVA

View previous topic View next topic Go down

HANDLING DATES IN JAVA

Post by Sirex on Thu Aug 20, 2015 11:42 am

1. Java Date Examples

Few examples to work with Date APIs.
Convert Date to String.

Code:
SimpleDateFormat sdf = new SimpleDateFormat ("dd/M/yyyy" );


String date = sdf.format (new Date()) ;


System.out.println (date);
 //15/10/2013



 Convert String to Date.


Code:
SimpleDateFormat sdf = new SimpleDateFormat ("dd-M-yyyy hh:mm:ss" ) ;


String dateInString = "31-08-1982


10:20:56" ;


Date date = sdf.parse[size=13](dateInString );


System.out.println ( date) ; 
//Tue






OUTPUT


Aug 31 10:20:56 SGT 1982



 Get current date time


Code:
SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy/MM/dd
HH:mm:ss" );
Date date = new Date () ;
System.out.println (dateFormat. format (date )) ;
//2013/10/15

[size]



16:16:39

Convert Calendar to Date


[/size]
Code:
Calendar calendar = Calendar.getInstance() ;

Date date = calendar. getTime () ;
[size]


2. Java Calendar Examples

Few examples to work with Calendar APIs.


Get current date time


[/size]
Code:
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy MMM dd HH:mm:ss" );

Calendar calendar = new GregorianCalendar (2013 , 0, 31) ;

System.out.println ( sdf.format(calendar. getTime ())) ;

[size]



Output
2013 Jan 31 00:00:00


Simple Calendar example


[/size]
Code:
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy MMM dd HH:mm:ss" );

Calendar calendar = new [size=13]GregorianCalendar (2013 , 1, 28, 13,24 ,56 );

int year = calendar.get (Calendar.YEAR) ;

int month = calendar.get(Calendar .MONTH );
// Jan = 0, dec = 11

int dayOfMonth = calendar.get

(Calendar.DAY_OF_MONTH ) ;

int dayOfWeek = calendar.get(Calendar .DAY_OF_WEEK );

int weekOfYear = calendar.get(Calendar .WEEK_OF_YEAR );

int weekOfMonth = calendar.get(Calendar .WEEK_OF_MONTH );

int hour = calendar.get(Calendar .HOUR ); // 12 hour clock




Code:
Hour clock

int minute = calendar.get(Calendar .MINUTE );

int second = calendar.get(Calendar .SECOND );

int millisecond = calendar.get(Calendar .MILLISECOND) ;


System.out.println ( sdf.format(calendar.getTime ())) ;

System.out.println ( "year \t\t : " + year) ;

System.out.println ( "month \t\t : " + month );

System. out .println ( "dayOfMonth \t : " + dayOfMonth) ;

System. out .println ( "dayOfWeek \t : " + dayOfWeek) ;

System. out .println ( "weekOfYear \t : " + weekOfYear );
Code:



System. out .println ( "weekOfMonth \t : " + weekOfMonth );


System. out .println ( "hour \t\t : " + hour );


System. out .println ( "hourOfDay \t : " + hourOfDay) ;


System. out .println ( "minute \t\t : " + minute) ;


System. out .println ( "second \t\t : "+ second) ;


System. out .println ( "millisecond \t : " + millisecond );


[size]


Output

2013 Feb 28 13: 24: 56
year : 2013
month : 1
dayOfMonth : 28
dayOfWeek  : 5
weekOfYear : 9
weekOfMonth     : 5
hour : 1
hourOfDay : 13
minute : 24
second : 56
millisecond     : 0




 Set a date manually



[/size]
Code:
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy MMM dd HH:mm:ss" );
Calendar calendar = new GregorianCalendar (2013 , 1, 28, 13,24 ,56 );
System. out .println ( "#1. " + [size=13]sdf. format ( calendar.getTime ())) ;


//update a date


calendar. set( Calendar.YEAR , 2014 );


calendar. set( Calendar.MONTH , 11) ;


calendar. set( Calendar.MINUTE , 33) ;


System. out .println ( "#2. " + sdf.format ( calendar.getTime ())) ;

[size]





Output
#1. 2013 Feb 28 13:24:56
#2. 2014 Dec 28 13:33:56


Add or subtract from a date.


[/size]
Code:
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy MMM dd" ) ;


Calendar calendar = new GregorianCalendar (2013 , 10,28 );


System. out .println ( "Date : " + sdf.format ( calendar.getTime ()));

//add one month


calendar. add(Calendar. MONTH, 1 );

System.out.println ( "Date : " + sdf.format ( calendar.getTime ()));
[size][size]



//subtract 10 days


[/size][/size]
Code:
calendar. add(Calendar. DAY_OF_MONTH , - 10) ;
System.out.println ( "Date : " + sdf.format ( calendar.getTime ()));
[size][size]



Output
Date : 2013 Nov 28
Date : 2013 Dec 28
Date : 2013 Dec 18


Convert Date to Calendar


[/size][/size]
Code:

SimpleDateFormat sdf = new SimpleDateFormat ("dd-M-yyyy hh:mm:ss" ) ;

String dateInString = "22-01-2015 10:20:56" ;

Date date = sdf.parse (dateInString );

Calendar calendar = Calendar.getInstance() ;

calendar.setTime( date) ;

Sirex
moderator
moderator

Posts : 4
Points : 8522
Reputation : 0
Join date : 2015-08-20

View user profile

Back to top Go down

Re: HANDLING DATES IN JAVA

Post by Admin on Thu Aug 20, 2015 10:58 pm

Nice one bro.. but can you please post d basics for does who dont have any idea...tnz
avatar
Admin
Admin
Admin

Posts : 83
Points : 9047
Reputation : 3
Join date : 2015-07-22

View user profile http://droid.1talk.net

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum