Java Calendar get field values
import java.text.DateFormatSymbols; import java.util.Calendar; public class Main { public static void main(String[] args) { Calendar gCal = Calendar.getInstance(); // Month is based upon a zero index, January is 0, int month = gCal.get(Calendar.MONTH) + 1; int day = gCal.get(Calendar.DATE); int yr = gCal.get(Calendar.YEAR); String dateStr = month + "/" + day + "/" + yr; System.out.println(dateStr);/*from ww w.j a va 2 s . c om*/ int dayOfWeek = gCal.get(Calendar.DAY_OF_WEEK); // Print out the integer value for the day of the week System.out.println(dayOfWeek); int hour = gCal.get(Calendar.HOUR); int min = gCal.get(Calendar.MINUTE); int sec = gCal.get(Calendar.SECOND); System.out.println(hour + ":" + min + ":" + sec); } }
import java.util.Calendar; public class Main { public static void main(String[] args) { Calendar now = Calendar.getInstance(); /*from w ww. j a va 2 s .c om*/ //get current date, year and month System.out.println("Current Year is : " + now.get(Calendar.YEAR)); //month start from 0 to 11 System.out.println("Current Month is : " + (now.get(Calendar.MONTH) + 1 )); System.out.println("Current Date is : " + now.get(Calendar.DATE)); //get current time information System.out.println("Current Hour in 12 hour format is : " + now.get(Calendar.HOUR)); System.out.println("Current Hour in 24 hour format is : " + now.get(Calendar.HOUR_OF_DAY)); System.out.println("Current Minute is : " + now.get(Calendar.MINUTE)); System.out.println("Current Second is : " + now.get(Calendar.SECOND)); System.out.println("Current Millisecond is : " + now.get(Calendar.MILLISECOND)); //display full date time System.out.println("Current full date time is : " + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR) + " " + now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND) + "." + now.get(Calendar.MILLISECOND) ); } }