java将指定日期时分秒改变为当前日期时间时分秒



//测试方法
  @org.junit.Test
  public void handle() {
    Calendar now = Calendar.getInstance();
    Date date = DateUtils.strToDateTime("2014-11-23 14:22:34");
    Date dates = new Date(now.get(Calendar.YEAR) - 1900, now.get(Calendar.MONTH), 
now.get(Calendar.DAY_OF_MONTH), date.getHours(), date.getMinutes(), date.getSeconds());
    System.out.println(DateUtils.date2Str(date));
    System.out.println(DateUtils.date2Str(dates));
  }


日期工具类



//字符串转时间  
  public static Date strToDateTime(String dateStr) {
    Date date = null;
    SimpleDateFormat strToDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
      date = strToDate.parse(dateStr);
    } catch (ParseException e) {
      e.printStackTrace();
      return null;
    }
    return date;
  }

//时间转字符串
  public static String date2Str(Date date) {
    SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String str = sdFormat.format(date);
    return str;
  }






你可能感兴趣的:(java将指定日期时分秒改变为当前日期时间时分秒)