Convert string to Date in java

前端 未结 6 1408
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 02:30

I\'m trying to parse a string to a date field in an android application but I can\'t seem to get it correct. Here is the string I\'m trying to convert to a date \"03/26/2012

6条回答
  •  死守一世寂寞
    2020-11-30 02:39

    This code will help you to make a result like FEB 17 20:49 .

        String myTimestamp="2014/02/17 20:49";
    
        SimpleDateFormat form = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        Date date = null;
        Date time = null;
        try 
        {
            date = form.parse(myTimestamp);
            time = new Date(myTimestamp);
            SimpleDateFormat postFormater = new SimpleDateFormat("MMM dd");
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            String newDateStr = postFormater.format(date).toUpperCase();
            String newTimeStr = sdf.format(time);
            System.out.println("Date  : "+newDateStr);
            System.out.println("Time  : "+newTimeStr);
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    

    Result :

    Date : FEB 17

    Time : 20:49

提交回复
热议问题