Home  >  Article  >  Java  >  Java example - get current time

Java example - get current time

黄舟
黄舟Original
2017-02-18 09:51:141209browse

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小时制) 
    } }

The output result of the above code is:

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

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)!


Statement:
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