Java obtains the current date and time
1. The specific implementation method is as follows:
/*** @ Title: DateTime. java * @ Package: com. you. model * @ Description: Get the current date and time * @ Author: youhaidong * @ date: 11:07:10, January 1, April 4, 2014 * @ Version V1.2.3 */package com. you. model; import java. util. calendar;/*** @ Class Name: DateTime * @ Description: gets the current date and time in the format of: YYYY-MM-DD HH: MM: SS * @ Author: youhaidong * @ date: April 4, 2014 11:07:10 */public class DateTime {public static String findDateTime () {/*** instantiate an object calendar */Calendar calendar = C Alendar. getInstance ();/*** get year */int year = calendar. get (Calendar. YEAR);/*** get month */int month = calendar ar. get (Calendar. MONTH) + 1;/*** get date */int day = calendar. get (Calendar. DATE);/*** get hour */int hour = calendar. get (Calendar. HOUR_OF_DAY);/*** get minute */int minute = calendar. get (Calendar. MINUTE);/*** get seconds */int second = calendar. get (Calendar. SECOND);/*** splicing date */String date = year + "-" + (month <10? "0" + month: month) + "-" + (day <10? "0" + day: day);/*** stitching time */String time = (hour <10? "0" + hour: hour) + ":" + (minute <10? "0" + minute: minute) + ":" + (second <10? "0" + second: second);/*** splicing date and time */String dateTime = date + "" + time; /*** return result */return dateTime;}/*** @ Title: main * @ Type: DateTime * @ date: 11:07:10, January 1, April 4, 2014 * @ Description: * @ param args */public static void main (String [] args) {/*** call the date and time Method */String str = findDateTime (); /*** print the result */System. out. println ("current date and time:" + str );}}
2. instance results
Current date and time: 23:19:28