Home > Java > javaTutorial > body text

Java example - get current time

黄舟
Release: 2017-02-18 09:51:14
Original
1172 people have browsed it

The following example demonstrates how to use the format(date) method of the Date class and SimpleDateFormat class to output the current time:

/*
 author by w3cschool.cc
 文件名:Main.java
 */import java.text.SimpleDateFormat;import java.util.Date;public class Main{
    public static void main(String[] args){
        
        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间 
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");// a为am/pm的标记  
        Date date = new Date();// 获取当前时间 
        System.out.println("现在时间:" + sdf.format(date)); // 输出已经格式化的现在时间(24小时制) 
    } }
Copy after login

The output result of the above code is:

现在时间:2015-03-27 21:27:28 下午
Copy after login

The above is the Java example - getting the content of the current time. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!