java计算某一时间点开始后的多少分钟的时间

描述:某一时间点开始,增加若干分钟后的得到的时间,如:计算考试的开考时间(2018-04-01 10:00)和考试时长100分钟,计算考试结束时间

/**
	 * 日期添加到分钟得到新时间(用于计算考试时间段)
	 * @param day 开始时间
	 * @param x		相隔分钟数(如开考的时间)
	 * @return
	 */
	public static String addDateMinut(String day, int x) {
		//入参的格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");// 24小时制  
        //出参的格式
        SimpleDateFormat newFormat = new SimpleDateFormat("HH:mm");// 24小时制  
        Date date = null;  
        try {  
            date = format.parse(day);  
        } catch (Exception ex) {  
            ex.printStackTrace();  
        }  
        if (date == null)  
            return "";  
        System.out.println("front:" + format.format(date));  
        Calendar cal = Calendar.getInstance();  
        cal.setTime(date);  
        cal.add(Calendar.MINUTE, x);// 24小时制  
        //得到结算后的结果 yyyy-MM-dd HH:mm
        date = cal.getTime();  
        System.out.println("after:" + format.format(date));  
        cal = null;  
        //转换结果的格式 HH:mm
        return newFormat.format(date);  
  
    }  

结果为:11:40

猜你喜欢

转载自blog.csdn.net/qq_35804654/article/details/79914468