Java gets the current time of the system

Get time year, month, day hour, minute, second

  // 获取时间 年、月、日 小时、分钟、秒
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  String date = sdf.format(new Date());

Get time year, month, day

 // 获取时间 年、月、日
 SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
 String date2 = sdf2.format(new Date());

Get time hour, minute, second

 // 获取时间 小时、分钟、秒
 SimpleDateFormat sdf3 = new SimpleDateFormat("hh:mm:ss");
 String date3 = sdf3.format(new Date());

get time seconds

// 获取时间 秒
 SimpleDateFormat sdf4 = new SimpleDateFormat("ss");
 String date4 = sdf4.format(new Date());

all codes

 // 获取时间 年、月、日 小时、分钟、秒
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        // 获取时间 年、月、日
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        // 获取时间 小时、分钟、秒
        SimpleDateFormat sdf3 = new SimpleDateFormat("hh:mm:ss");
        // 获取时间 秒
        SimpleDateFormat sdf4 = new SimpleDateFormat("ss");
        String date = sdf.format(new Date());
        String date2 = sdf2.format(new Date());
        String date3 = sdf3.format(new Date());
        String date4 = sdf4.format(new Date());

        System.out.println("时间 年、月、日 小时、分钟、秒:"+date);
        System.out.println("时间 年、月、日:"+date2);
        System.out.println("时间 小时、分钟、秒:"+date3);
        System.out.println("时间 秒:"+date4);

Guess you like

Origin blog.csdn.net/weixin_46409629/article/details/129678644