This article illustrates how Java gets time, months, and days. Share to everyone for your reference. The implementation method is as follows:
Package Com.date.demo;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;
Import Java.util.Date; public class Datedemo {public static void main (String args[]) {System.out.println ("---------------to get the date of the current time----
-------------");
Getmonthday ();
System.out.println ("---------------to obtain the date of the custom Time-----------------");
Getmonthday2set ();
/** * Gets the custom time month/day/private static void Getmonthday2set () {String datestr = "2013-11-10 18:45:39";
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Calendar cal = Calendar.getinstance ();
Date dt = null;
try {dt = Sdf.parse (DATESTR);
Cal.settime (DT);
catch (ParseException e) {//TODO auto-generated catch block E.printstacktrace ();
int year = Cal.get (calendar.year);
int month = Cal.get (calendar.month) + 1;
int day = Cal.get (calendar.day_of_month); int hour = Cal.get (Calendar.hour_of_day);
int minute = Cal.get (Calendar.minute);
int second = Cal.get (Calendar.second);
System.out.println ("= = = = = = =" + year);
System.out.println ("= = = Month = = =" + month);
System.out.println ("= = = Day = = =" + days);
System.out.println ("= = = = When = =" + hour);
System.out.println ("= = = = = = = = =" + minute);
System.out.println ("= = = Second = = =" + second);
/** * Gets the current time year/month/private static void Getmonthday () {Calendar cal = Calendar.getinstance ();
int year = Cal.get (calendar.year);
int month = Cal.get (calendar.month) + 1;
int day = Cal.get (calendar.day_of_month);
int hour = Cal.get (Calendar.hour_of_day);
int minute = Cal.get (Calendar.minute);
int second = Cal.get (Calendar.second);
System.out.println ("= = = = = = =" + year);
System.out.println ("= = = Month = = =" + month);
System.out.println ("= = = Day = = =" + days);
System.out.println ("= = = = When = =" + hour);
System.out.println ("= = = = = = = = =" + minute); System.out.println("= = = Second = = =" + second); }
}
I hope this article will help you with your Java programming.