unhandled exception type ParseException [closed]
Asked Answered
N

3

6

I'm using this part of code in my app :

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
Date qdate = new GregorianCalendar(0,0,0).getTime();
try {
    qdate = sdf.parse(dt);
} catch (ParseException e) {
    e.printStackTrace();
}

but Eclipse throws an error saying :

Unhandled exception type ParseException

What is the problem here? Do u need me to post the whole code ? Thnx in advance !

Nonesuch answered 26/7, 2012 at 8:21 Comment(2)
Code snippet works fine here. Try to clean and re-build your project.Shitty
Didn't work out :( The weird thing is that i wrote this part of code in a function and worked fine. But when i wrote another function below , then the error occured .. Is that a clue ??Nonesuch
T
16

See below code

        String date = "Sat, 23 Jun 2012 00:00:00 +0000";
        try {
            SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
            SimpleDateFormat df2 = new SimpleDateFormat("dd/MM/yy");
            date = df2.format(date));
        } catch (java.text.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
Theurgy answered 26/7, 2012 at 8:27 Comment(3)
Yeah , that was the problem :) I had wrong package imported :) So , instead of doing this , i just changed the package imported :) Thnx for the guidance though !!! :)Nonesuch
Already did.. I cant vote yet , though .. :) Im only 4 reputation , and to be able to vote requires 15 rep :(Nonesuch
Have a look at my code, and see if you can help me. SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = tvData.getText().toString() + " " + tvHora.getText().toString(); Date d = parser.parse(dateStr); valor.put("data", d.getTime());Bicuspid
O
0

java.time

It’s time to show the modern way of doing this. Use java.time, the modern Java date and time API.

    String dt = "2020-07-24T11:03:12Z";
    try {
        Instant i = Instant.parse(dt);
        System.out.println(i);
    } catch (DateTimeParseException dtpe) {
        System.err.println(dtpe);
    }

Output is:

2020-07-24T11:03:12Z

I am exploiting the fact that your format is ISO 8601 and the classes of java.time parse the most common variants of ISO 8601 as their default, that is, without any explicit formatter.

The exception comes into play if the string is not a valid ISO 8601 date and time in UTC. For example:

    String dt = "Not a valid date-time";

java.time.format.DateTimeParseException: Text 'Not a valid date-time' could not be parsed at index 0

The classes SimpleDateFormat and Date are poorly designed and long outdated, the former in particular notoriously troublesome. I recommend that nobody uses them anymore. The modern API is so much nicer to work with.

Links

Overshadow answered 24/7, 2020 at 11:7 Comment(0)
E
-1
Calendar c = Calendar.getInstance();

SimpleDateFormat df = new SimpleDateFormat("DD-MM-YYYY");

String Currentdatestr = df.format(c.getTime());

//get current date in String

Date date = df.parse(Currentdatestr);

//String to parse Date Format
Edaphic answered 30/5, 2019 at 11:10 Comment(1)
Wrong. Today is July 24, and your code just gave me a string of 206-07-2020 and a Date of Mon Dec 30 00:00:00 CET 2019. Furthermore Calendar, SimpleDateFormat and Date are poorly designed classes and outdated long before this answer was posted in 2019.Overshadow

© 2022 - 2024 — McMap. All rights reserved.