Date 获取当前时间和指定时间

来源:互联网 发布:淘宝游戏王店铺推荐 编辑:程序博客网 时间:2024/05/29 09:31

Java获取当前时间

import java.text.SimpleDateFormat;import java.util.Date;public class Datetest {//    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd 20:00:00");    public static void main(String[] args) {        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd 00:00:00");        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");        Date date = new Date();        //String time = simpleDateFormat.format(date);        System.out.print(date);        System.out.print(time    }}
  1. SimpleDateFormat指定时间格式 2017-08-05 14:08:00,2017-08-05 20:00:00

  2. System.out.print(date);直接输出当前时间,格式Sat Aug 05 14:05:31 CST 2017

 Calendar currentDate = new GregorianCalendar();        currentDate.set(Calendar.HOUR_OF_DAY, 0);        currentDate.set(Calendar.MINUTE, 0);        currentDate.set(Calendar.SECOND, 0);        String startTime = simpleDateFormat.format(currentDate.getTime());

1.2017-08-05 00:00:00 第二种方式输出每天的开始时间。