现在有yyyymmddhhmmss格式的日期
我想格式化成yyyy/mm/dd hh:mm
和yyyy/mm/dd
如果传入第一种的格式的就格式化成第一种
如果传入的参数是第二种的话,格式成第二种
谢谢大家

解决方案 »

  1.   

    format(yyyy/mm/dd hh:mm)
    format(yyyy/mm/dd )就可以 了
      

  2.   

    传入参数不知道能不能根据长度判断import java.sql.Timestamp;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;public class MyTest1 {    /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            MyTest1 test = new MyTest1();
            Calendar cal = test.stringToCal("2009080415525", "yyyyMMddHHmmss");
            String str = test.calToString(cal, "yyyy-MM-dd HH:mm:ss");
            System.out.println(str);
        }    /**
         * 将字符串日期转换成Calendar型
         * @param datestring
         * @param format
         * @return
         */
        public static Calendar stringToCal(String datestring,String format)
        {
          Calendar cal = Calendar.getInstance();
          SimpleDateFormat df = new SimpleDateFormat ( format );
          try
          {
              if (datestring!=null && datestring.length()!=0)
                  cal.setTime(new Timestamp(df.parse(datestring).getTime()));
          }
          catch (Exception ex)
          {
              ex.printStackTrace();
              cal =null;
          }
          return cal;
        }
        
        /**
         * 将Calendar型转换为String型,指定日期格式输出
         * @param source_cal
         * @param format
         * @return
         */
        public static String calToString(Calendar source_cal,String format)
        {
          String dateString = null;      try{
            if (source_cal!=null){
              SimpleDateFormat sdf = new SimpleDateFormat (format);
              sdf.setCalendar(source_cal);
              dateString = sdf.format(source_cal.getTime());
            }
          }catch (Exception ex)
          {
            System.out.println(ex.getMessage());
            dateString = null;
          }     
          return dateString;
          
        }
    }
      

  3.   

    1、先转为日期Calendar,再用DateFormat进行格式化输出2、直接拆分字符串
      

  4.   

    使用此方法:SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm");import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Locale;
    public class TestTime {/**
    * @param args
    */
    public static void main(String[] args) {
       String s1 = "yyyyMMddHH";
       String s2 = "2008080808";
      
       String s3 = "2008080807";
      
       TestTime tt = new TestTime();
       tt.timeop(s2,s1,s3);
    }
    //将日期转换为毫秒数
    public long timeop(String s1,String pattern,String s3) {
       // 传入的参数要与yyyyMMddHH的格式相同 "yyyyMMddHH"
       SimpleDateFormat simpledateformat = new SimpleDateFormat(pattern);
       Date date2 = null;
       Date date3 = null;   try {
        date2 = simpledateformat.parse(s1);
        date3 = simpledateformat.parse(s3);
       } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }// 将参数按照给定的格式解析参数
       System.out.println(date2.getTime());
       System.out.println(date3.getTime());
       System.out.println(date2.getTime() - date3.getTime());
       return date2.getTime();
    }}Date dd = new Date(m) ;//通过毫秒数构造Date对象
    // 时间格式设置
       SimpleDateFormat dat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
       Date d = new Date();   System.out.println(d.toString() + " *** " + dat.format(d));
      

  5.   

    补充说明,传入的是字符串
    就是根据传入字符串的格式进行格式化
    回二楼的
    我要的是yyyy/MM/dd HH 只是这中形式的话,能够用那个函数吗
    我是了一下,好像出错
      

  6.   

    判断值,我想你应该会吧,可以在值传入的时候就判断
    if(str.length()==8){
    ...
    }if(str.length()==10){
    ...
    }so,so.
      

  7.   

    2楼基本已经写出来了,就用java.text.SimpleDateFormat中的构造方法就可以实现,具体建议你去看看API
      

  8.   

    分别生成两个SimpleDateFormat对象就可以了