日期和时间

来源:互联网 发布:mac用什么flash player 编辑:程序博客网 时间:2024/06/06 18:50

1.代码   //日期时间

import java.util.*;

public class TestTime {

public static void main(String[] args) {

//获取本机的当前日期和时间

     Date nowTime=new Date();

     System.out.println(nowTime);

     //初始化一个日历对象

     Calendar ca =Calendar.getInstance();

     ca.setTime(new Date());

     //获取当前的年月日

     int year=ca.get(Calendar.YEAR),

      month=ca.get(Calendar.MONTH)+1,

      day=ca.get(Calendar.DAY_OF_MONTH);

     System.out.println("现在的时间是:");

     System.out.print(" "+year+"年"+" "+month+"月"+day+"日");

     //获取当天的具体时间 时分秒

     int hour=ca.get(Calendar.HOUR_OF_DAY),

         minute=ca.get(Calendar.MINUTE),

         second=ca.get(Calendar.SECOND);

     System.out.println(""+hour+"时"+" "+minute+"分"+" "+second+"秒"+" ");

}

}

2.运行截图

原创粉丝点击