Date class
Java provides the Date class that is available in java.util package, this class gives the current date and time.
public class DateTime
{
public static void main(String[] args)
{
Date d = new Date();
System.out.println(d);
}
}
Output
Tue Jun 24 23:08:29 IST 2014
You can use
getDay()
getMonth()
getYear()
getMinutes()
getHour()
getSeconds()
getTime() //it will give in milliseconds
Eg: Date d = new Date();
d.getDay(); similarly u can use any date class get methods or set methods using the Date class reference.
import java.util.Date;
public class C
{
public static void main(String[] args)
{
Date d = new Date();
System.out.println(d);
System.out.println("*******************");
SimpleDateFormat ss = new SimpleDateFormat("E dd-MM-YYYY hh:mm a");
System.out.println(ss.format(d));
}
}
Output
*******************
Tue 30-09-2014 05:30 PM
Simple DateFormat format codes:
To specify the time format use a time pattern string. In this pattern, all ASCII letters are reserved as pattern letters, which are defined as the following:
Character Description Example
G Era designator AD
y Year in four digits 2001
M Month in year July or 07
d Day in month 10
h Hour in A.M./P.M. (1~12) 12
H Hour in day (0~23) 22
m Minute in hour 30
s Second in minute 55
S Millisecond 234
E Day in week Tuesday
D Day in year 360
F Day of week in month 2 (second Wed. in July)
w Week in year 40
W Week in month 1
a A.M./P.M. marker PM
k Hour in day (1~24) 24
K Hour in A.M./P.M. (0~11) 10
z Time zone Eastern Standard Time
Calender Class
This class is used to get the system date and time. Even this class is also present in the java.utli package.
public class DateTime
{
public static void main(String[] args)
{
Calendar cal = Calendar.getInstance();
int day = cal.DATE;
int mon = cal.MONTH;
int yer = cal.YEAR;
System.out.println("Date is "+day+" - "+mon+" - "+yer);
int hour = cal.HOUR;
int min = cal.MINUTE;
int sec = cal.SECOND;
System.out.println("Time is "+hour+" - "+min+" - "+sec);
}
}
ALSO SEARCH:
"java calendar class"
"calendar class in java 8"
"difference between date and calendar class in java"
"calendar class in java get day"
"date class in java"
"calendar class in java w3schools"
"java 11 calendar"
"java calendar set"