IT Certificaions

Google Cloud Platform

Thursday, May 16, 2013

Date using Java: create Data using year, month, day, hour, minute, seconds in Java

Creating  custom date using java
1) Using java.util.Date
 int year=2013;
int month=5;   // months starts from 0 to 11, as 0 stands January and 11 stands for December
int day=15;
int hour=12;
int minute=58;
int seconds=47;

*** Date utilDateObj=new Date(year,month,day,hour,minute,seconds);

***  This is depricated.

We recommend to use Calander as given below:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);

calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, seconds);

Date lastUpdatedDate = calendar.getTime();