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 !

Tasos Moustakas
  • 308
  • 3
  • 12
  • 22
  • 1
    Code snippet works fine here. Try to clean and re-build your project. – Robert Jul 26 '12 at 08:25
  • 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 ?? – Tasos Moustakas Jul 26 '12 at 08:29

3 Answers3

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();
        }
Nirali
  • 13,571
  • 6
  • 40
  • 53
  • 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 !!! :) – Tasos Moustakas Jul 26 '12 at 08:33
  • Already did.. I cant vote yet , though .. :) Im only 4 reputation , and to be able to vote requires 15 rep :( – Tasos Moustakas Jul 26 '12 at 08:39
  • 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());` – baTimá Oct 11 '12 at 19:59
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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
-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
Nehru
  • 1
  • 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. – Ole V.V. Jul 24 '20 at 11:00