怎么样将这样的字符串“Jan 10 19:19:28 2012”转换为date格式。我这样转换有问题,但不知道格式哪里错了:String date = "Jan 10 19:19:28 2012";
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd HH:mm:ss yyyy");
long currdate = formatter.parse(date).getTime();

解决方案 »

  1.   


    public static void main(String[] args) {
    String date = "Jan 10 19:19:28 2012";
    long d = Date.parse(date);
    System.out.println(d);
    SimpleDateFormat formatter = new SimpleDateFormat(
    "yyyy MM dd HH:mm:SS");
    String currdate;
    currdate = formatter.format(d);
    System.out.println(currdate);

    }
      

  2.   


    public class TestDate {
    public static void main(String[] args) throws Exception
    {

    String date = "05 02 12:12:12 2012";
    SimpleDateFormat formatter = new SimpleDateFormat("MM dd HH:mm:ss yyyy");
    long currdate = formatter.parse(date).getTime(); System.out.println(currdate);

    }
    }
    前后格式要对应,兄弟
      

  3.   

    楼上的Date.parse 我之前是用这方法,但为过时方法,我们代码中不能用过时的方法。long d = Date.parse(date);
      

  4.   

    3楼的long d = Date.parse(date);方法是过时的,可惜我们的代码中不提倡使用过时的方法。
      

  5.   


    import java.text.SimpleDateFormat;
    import java.util.Date;public class Test {
    public static void main(String[] aa) {
    SimpleDateFormat dateformat1 = new SimpleDateFormat(
    "yyyy-MM-dd HH:mm:ss E");
    String a1 = dateformat1.format(new Date());
    System.out.println("时间2:" + a1);
    System.out.println(new Date().getYear() + 1900); SimpleDateFormat dateformat2 = new SimpleDateFormat(
    "yyyy年MM月dd日 HH时mm分ss秒 E ");
    String a2 = dateformat2.format(new Date());
    System.out.println("时间2:" + a2);
    }
    }
    LZ自己看着用吧
      

  6.   


    public class TestDate {
        public static void main(String[] args) throws Exception
        {
            
           String str="Jan 02 12:12:12 2012";
            SimpleDateFormat formatter = new SimpleDateFormat("MMM dd HH:mm:ss yyyy",Locale.ENGLISH);
            Date ts = formatter.parse(str);
            SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy MM HH:mm:SS");
            System.out.print(formatter1.format(ts));
        }
    }
      

  7.   

    说实话我是google加API做出来的