简体   繁体   中英

Formating Date and Time in specific case on Android

I've searched a lot, but nothing seams to help me... I have built a string out of many resources that in the end looks like this (I have used some substring methods and things like that):

xxxa="16.Aug.2012 07:15:00"

and I want to parse it into date so I can compare it with current time. So, I use those lines:

Date datex=null;
DateFormat formatter = new SimpleDateFormat("dd.Mmm.yyyy hh:mm:ss");
try {
            datex = (Date)formatter.parse(xxxa);
            //Log.w("TIME: ", datex.toString());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

From my point of view, those 2 things match and should be parsed normaly.. or should it?

I always get an error like this:

java.text.ParseException: Unparseable date: "16.Aug.2012 07:00:00" at Java.text.DateFormat.parse(DateFormat.java:626) ........

I've tried to change positions of letters, to change where something will be.. but still does not fit. I can change that string to look like anything... I just want it to look like a real time, so I can compare time now and that time...

Anyone ??

As per SimpleDateFormat javadoc

M Month in year

m Minute in hour

 String dateStr = "16.Aug.2012 07:15:00";
        Date datex = null;
        DateFormat formatter = new SimpleDateFormat("dd.MMM.yyyy hh:mm:ss");
        try {
            datex = (Date) formatter.parse(dateStr);
            System.out.println(datex);
            //Log.w("TIME: ", datex.toString());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM