Java obtains the current time

Source: Internet
Author: User

Java obtains the current time


1. Use the Calendar in Java to obtain the current time

The specific implementation is as follows:

/*** @ Title: NowTime. java * @ Package: com. you. model * @ Description: Get the current time * @ Author: youhaidong * @ date: 10:38:20, January 1, April 4, 2014 * @ Version V1.2.3 */package com. you. model; import java. util. calendar;/*** @ Class Name: NowTime * @ Description: * @ Author: youhaidong * @ date: april 4, 2014 10:38:20 */public class NowTime {/***** @ Title: findTime * @ Type: NowTime * @ date: April 4, 2014 10:44:35 * @ Description: obtain the current system time * @ return */public s Tatic String findTime () {Calendar calendar = Calendar ar. getInstance ();/*** hour */int hour = calendar. get (Calendar. HOUR);/*** minute */int minute = calendar. get (Calendar. MINUTE);/*** seconds */int second = calendar. get (Calendar. SECOND);/*** splicing time (hour, minute, and SECOND) */String time = (hour <10? "0" + hour: hour) + ":" + (minute <10? "0" + minute: minute) + ":" + (second <10? "0" + second: second);/*** returns the hour, minute, and second */return time;}/*** @ Title: main * @ Type: NowTime * @ date: april 4, 2014 10:38:20 * @ Description: Test findTime () * @ param args */public static void main (String [] args) {/*** call the findTime Method */String nowsday = findTime ();/*** print the result */System. out. println ("current time:" + nowsday );}}


2. Implementation result

Current Time: 10: 53: 04



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.