如何将"140654"字符串; 转化为小时并减去3分钟!

解决方案 »

  1.   

    SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
    Date date = sdf.parse("140220");转换成Date后可以用Calendar函数去转换
      

  2.   

    public static void main(String[] arg) throws ParseException {
    String srcStr = "140654";
    SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
    Date date = sdf.parse(srcStr);
    date.setMinutes(date.getMinutes() - 3);
    SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
    System.out.println(sdf2.format(date));
    }
      

  3.   

    我的思路是:
    (1)把"140654" 转化为日期格式;
    (2)参考如下函数:
      public static java.util.Date getDateBeforeMinute(Date d, int minutes)
        {
            Calendar now = Calendar.getInstance();
            now.setTime(d);
            now.set(Calendar.DATE, now.get(Calendar.MINUTE) - minutes);
            return now.getTime();
        }
      

  4.   


    public static void main(String[] arg) {
            String srcStr = "140654";
            SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
            Calendar calendar = new GregorianCalendar();
    try {
    calendar.setTime(sdf.parse(srcStr));
    calendar.add(calendar.MINUTE, -3);//减3分钟
    } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
        }
      

  5.   


    优化了下:
            String srcStr = "140654";
            DateFormat sdf = new SimpleDateFormat("HHmmss");
            sdf.parse(srcStr);
            Calendar date = sdf.getCalendar();
            date.set(Calendar.MINUTE, date.get(Calendar.MINUTE) - 3);
            SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
            System.out.println(sdf2.format(date.getTime()));
      

  6.   


    优化了下:
            String srcStr = "140654";
            DateFormat sdf = new SimpleDateFormat("HHmmss");
            sdf.parse(srcStr);
            Calendar date = sdf.getCalendar();
            date.set(Calendar.MINUTE, date.get(Calendar.MINUTE) - 3);
            SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
            System.out.println(sdf2.format(date.getTime()));
    +1