dabocaiqq
dabocaiqq
采纳率100%
2019-10-05 14:37

悬赏的问题,Java语言怎么获取当前的系统时间和日期,并且输出,谢谢

5

悬赏的问题,Java语言怎么获取当前的系统时间和日期,并且输出,谢谢
要有详细代码。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

6条回答

  • xiaoxiao_2020 xiaoxiao_2020 2年前

    package com.fh.util;

    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;

    public class DateUtil {
    private final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");

    private final static SimpleDateFormat sdfDay = new SimpleDateFormat(
            "yyyy-MM-dd");
    
    private final static SimpleDateFormat sdfDays = new SimpleDateFormat(
    "yyyyMMdd");
    
    private final static SimpleDateFormat sdfTime = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");
    
    private final static SimpleDateFormat sdfDateTime = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm");
    
    private final static SimpleDateFormat sdfTime2 = new SimpleDateFormat(
            "HH:mm:ss");
    /**
     * 获取YYYY格式
     * 
     * @return
     */
    public static String getYear() {
        return sdfYear.format(new Date());
    }
    
    /**
     * 获取YYYY-MM-DD格式
     * 
     * @return
     */
    public static String getDay() {
        return sdfDay.format(new Date());
    }
    
    /**
     * 获取YYYYMMDD格式
     * 
     * @return
     */
    public static String getDays(){
        return sdfDays.format(new Date());
    }
    
    /**
     * 获取YYYY-MM-DD HH:mm:ss格式
     * 
     * @return
     */
    public static String getTime() {
        return sdfTime.format(new Date());
    }
    
    /**
     * 获取HH:mm:ss格式
     * 
     * @return
     */
    public static String getTime2() {
        return sdfTime2.format(new Date());
    }
    
    /**
    * @Title: compareDate
    * @Description: TODO(日期比较,如果s>=e 返回true 否则返回false)
    * @param s
    * @param e
    * @return boolean  
    * @throws
    * @author luguosui
     */
    public static boolean compareDate(String s, String e) {
        if(fomatDate(s)==null||fomatDate(e)==null){
            return false;
        }
        return fomatDate(s).getTime() >= fomatDate(e).getTime();
    }
    
    public static boolean compareDate2(String s, String e) {
        if(fomatDate(s)==null||fomatDate(e)==null){
            return false;
        }
        return fomatDate(s).getTime() > fomatDate(e).getTime();
    }
    
    /**
    * @Title: compareDate
    * @Description: TODO(时间比较,如果s>=e 返回true 否则返回false)
    * @param s
    * @param e
    * @return boolean  
    * @throws
    * @author luguosui
     */
    public static boolean compareTime(String s, String e) {
        if(fomatTime(s)==null||fomatTime(e)==null){
            return false;
        }
        return fomatTime(s).getTime() >=fomatTime(e).getTime();
    }
    
    
    /**
    * @Title: compareDateTime
    * @Description: TODO(时间比较,如果s>=e 返回true 否则返回false)
    * @param s
    * @param e
    * @return boolean  
    * @throws
    * @author litianrui
     */
    public static boolean compareDateTime(String s, String e) {
        if(fomatDateTime(s)==null||fomatDateTime(e)==null){
            return false;
        }
        return fomatDateTime(s).getTime() >=fomatDateTime(e).getTime();
    }
    
    public static boolean compareDateTime2(String s, String e) {
        if(fomatDateTime(s)==null||fomatDateTime(e)==null){
            return false;
        }
        return fomatDateTime(s).getTime() >fomatDateTime(e).getTime();
    }
    
    /**
     * 格式化日期 yyyy-MM-dd
     * 
     * @return
     */
    public static Date fomatDate(String date) {
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return fmt.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 格式化日期yyyyMMdd 得到字符串
     * 
     * @return
     */
    public static String fomatDateStr(String date) {
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dates = fmt.parse(date);
            DateFormat fmts = new SimpleDateFormat("yyyyMMdd");
            return fmts.format(dates);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 格式化时间 HH:mm:ss
     * 
     * @return
     */
    public static Date fomatTime(String date) {
        DateFormat fmt = new SimpleDateFormat("HH:mm:ss");
        try {
            return fmt.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * 格式化日期时间 yyyy-MM-dd HH:mm:ss
     * 
     * @return
     */
    public static Date fomatDateTime(String date) {
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return fmt.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    
    /**
     * 校验日期是否合法
     * 
     * @return
     */
    public static boolean isValidDate(String s) {
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        try {
            fmt.parse(s);
            return true;
        } catch (Exception e) {
            // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
            return false;
        }
    }
    public static int getDiffYear(String startTime,String endTime) {
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        try {
            long aa=0;
            int years=(int) (((fmt.parse(endTime).getTime()-fmt.parse(startTime).getTime())/ (1000 * 60 * 60 * 24))/365);
            return years;
        } catch (Exception e) {
            // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
            return 0;
        }
    }
      /**
     * <li>功能描述:时间相减得到天数
     * @param beginDateStr
     * @param endDateStr
     * @return
     * long 
     * @author Administrator
     */
    public static long getDaySub(String beginDateStr,String endDateStr){
        long day=0;
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
        java.util.Date beginDate = null;
        java.util.Date endDate = null;
    
            try {
                beginDate = format.parse(beginDateStr);
                endDate= format.parse(endDateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            day=(endDate.getTime()-beginDate.getTime())/(24*60*60*1000);
            //System.out.println("相隔的天数="+day);
    
        return day;
    }
    
    /**
     * 得到n天之后的日期
     * @param days 
     * @return  yyyy-MM-dd HH:mm:ss
     */
    public static String getAfterDayDate(String days) {
        int daysInt = Integer.parseInt(days);
    
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
        Date date = canlendar.getTime();
    
        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdfd.format(date);
    
        return dateStr;
    }
    
    /**
     * 得到n天之后的日期
     * @param days 
     * @return  yyyy-MM-dd
     */
    public static String getAfterDayDate2(String days) {
        int daysInt = Integer.parseInt(days);
    
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
        Date date = canlendar.getTime();
    
        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdfd.format(date);
    
        return dateStr;
    }
    
    /**
     * 得到n天之后的日期的0点
     * @param days 
     * @return  yyyy-MM-dd 00:00
     */
    public static String getAfterDayDate3(String days) {
        int daysInt = Integer.parseInt(days);
    
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
        Date date = canlendar.getTime();
    
        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd 00:00");
        String dateStr = sdfd.format(date);
    
        return dateStr;
    }
    
    
    /**
     * 得到n天之后是周几
     * @param days
     * @return
     */
    public static String getAfterDayWeek(String days) {
        int daysInt = Integer.parseInt(days);
    
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
        Date date = canlendar.getTime();
    
        SimpleDateFormat sdf = new SimpleDateFormat("E");
        String dateStr = sdf.format(date);
    
        return dateStr;
    }
    
    
    
    /**
     * 获取每个月第一天 
     * YYYY-MM-DD格式
     * @return
     */
    public static String getFirstDay() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Date theDate = calendar.getTime();
    
        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
        gcLast.setTime(theDate);
        gcLast.set(Calendar.DAY_OF_MONTH, 1);
        String day_first = df.format(gcLast.getTime());
        return day_first;
    

    // StringBuffer str = new StringBuffer().append(day_first).append(" 00:00:00");
    // return str.toString();
    }

    /**
     * 获取每个月最后一天 
     * YYYY-MM-DD格式
     * 
     * @return
     */
    public static String getLastDay() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Date theDate = calendar.getTime();
        String s = df.format(theDate);
        return s;
    //  StringBuffer str = new StringBuffer().append(s).append(" 23:59:59");
    //  return str.toString();
    }
    
    // 获得当天0点时间  
    public static Date getTimesmorning() {  
        Calendar cal = Calendar.getInstance();  
        cal.set(Calendar.HOUR_OF_DAY, 0);  
        cal.set(Calendar.SECOND, 0);  
        cal.set(Calendar.MINUTE, 0);  
        cal.set(Calendar.MILLISECOND, 0);  
        return cal.getTime();  
    }  
    
    // 获得当天23点59分59秒时间  
    public static Date getTimesnight() {  
        Calendar cal = Calendar.getInstance();  
        cal.set(Calendar.HOUR_OF_DAY, 24);  
        cal.set(Calendar.SECOND, 0);  
        cal.set(Calendar.MINUTE, 0);  
        cal.set(Calendar.MILLISECOND, 0);  
        cal.add(Calendar.SECOND, -1);
        return  cal.getTime();  
    }  
    
    // 获得本周一0点时间  
    public static Date getTimesWeekmorning() {  
        Calendar cal = Calendar.getInstance();  
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);  
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);  
        return  cal.getTime();  
    }  
    
    // 获得本周日23点59分59秒时间  
    public  static Date getTimesWeeknight() {  
        Calendar cal = Calendar.getInstance();  
        cal.setTime(getTimesWeekmorning());  
        cal.add(Calendar.DAY_OF_WEEK, 7);  
        cal.add(Calendar.SECOND, -1);
        return cal.getTime();  
    }  
    
    // 获得本月第一天0点时间  
    public static Date getTimesMonthmorning() {  
        Calendar cal = Calendar.getInstance();  
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);  
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));  
        return  cal.getTime();  
    }  
    
    // 获得本月最后一天23点59分59秒时间  
    public static Date getTimesMonthnight() {  
        Calendar cal = Calendar.getInstance();  
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);  
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));  
        cal.set(Calendar.HOUR_OF_DAY, 24);  
        cal.add(Calendar.SECOND, -1);
        return cal.getTime();  
    }  
    
    /**
     * 获取YYYY-MM-DD格式
     * 
     * @return
     */
    public static String getymdDay(Date date ) {
        return sdfDay.format(date);
    }
    
    /**
     * 获取YYYY-MM-DD hh:ii格式
     * 
     * @return
     */
    public static String getymdDayTime(Date date ) {
        return sdfDateTime.format(date);
    }
    
    public static String getymdDayTime2(Date date ) {
        return sdfTime.format(date);
    }
    
    public static void main(String[] args) {
        System.out.println(getAfterDayDate2("-7"));
    }
    
    /**
     * 根据当前日期的规则字符以及数字n串形式获取前后n天的日期的规则字符串形式
     * @param currentDayStr
     * @return
     * @throws ParseException
     */
    public static String getPointDayByDateStr(String currentDayStr,int n) throws ParseException {
        Date date = sdfDay.parse(currentDayStr);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE,n);//把日期往前减少一天,若想把日期向后推一天则将负数改为正数
        date=calendar.getTime(); 
        return sdfDay.format(date);
    }
    
    
    
    /**
     * 日期转时间戳
     * @param date
     * @return
     */
    public static Integer transForMilliSecond(Date date){
        if(date==null) return null;
        return (int)(date.getTime()/1000);
    }
    
    
    /**
     * 日期字符串转时间戳
     * @param dateStr
     * @return
     */
    public static Integer transForMilliSecond(String dateStr){
        Date date = DateUtil.fomatDateTime(dateStr);
        return date == null ? null : DateUtil.transForMilliSecond(date);
    }
    

    }
    希望对你有帮助

    点赞 评论 复制链接分享
  • weixin_39302506 ++小猴子 2年前

    可以使用new Date(),或者Calendar
    1、使用 new Date()
    Date nowDate = new Date();
    System.out.println("格式化前:" + nowDate);
    //格式化
    SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String format = sim.format(nowDate);
    //输出
    System.out.println("格式化后:" + format);
    2、使用Calendar
    //获取当前时间
    Calendar calendar = Calendar.getInstance();
    //获取需要的时间格式,日期 年、月、日
    System.out.println(calendar.get(calendar.YEAR)+"年"+
    calendar.get(calendar.MONTH) + "月" +
    calendar.get(calendar.DATE) + "日");
    //时间 时分秒 HOUR_OF_DAY-24小时制,HOUR-12小时制
    System.out.println(calendar.get(calendar.HOUR_OF_DAY) + "时" +
    calendar.get(calendar.MINUTE) + "分" +
    calendar.get(calendar.SECOND) + "秒");
    //毫秒
    System.out.println(calendar.get(Calendar.MILLISECOND)+"毫秒");

    控制台输出结果:(https://img-ask.csdn.net/upload/201910/08/1570506506_188052.jpg)

    点赞 评论 复制链接分享
  • Bangser Faxon 2年前

    可以使用jdk1.8的时间Api

    
            //获取日期
            LocalDate date = LocalDate.now();
            System.out.println(date); //结果 2019-10-06
    
            //获取日期时间
            LocalDateTime dateTime = LocalDateTime.now();
            System.out.println(dateTime); //结果 2019-10-06T10:59:25.859
    
            //格式化
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String format = formatter.format(dateTime);
            System.out.println(format); //结果 2019-10-06 10:59:25
    
            //获取当前时间毫秒值
            long milli = Instant.now().toEpochMilli();
            System.out.println(milli); //结果 1570330765965
    
    
    点赞 评论 复制链接分享
  • windinwater 水里风 2年前

    SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); //y表示年,M表示月,d表示日;可以自定义输出的格式
    System.out.println(sdf.format(new Date()));//new Date()自动获取当前时间,SimpleDateFormat类用来格式化日期以便于输出展示

    点赞 评论 复制链接分享
  • qiangchen1990 qiangchen1990 2年前
                    Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH) + 1;
            int day = calendar.get(Calendar.DATE);
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            int minute = calendar.get(Calendar.MINUTE);
    
    点赞 评论 复制链接分享
  • weixin_43943977 Soul fragments 2年前

    new Date()函数获取时间即可,代码如下

    import java.util.Date;
    import java.text.SimpleDateFormat;

    public class simple{
    public static void main(String[] args) {
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
    System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
    }
    }

    点赞 评论 复制链接分享