0

I actually wnat to find difference between the two dates but get unparceable date error at the curent date :

String act_dateString;

         Calendar cal =  Calendar.getInstance();
          act_dateString  = cal.getTime().toLocaleString();


        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy/MM/dd");

        Date date_old = null;

            try {
                date_old = formatter1.parse(act_dateString);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

Logcat shows:

10-21 00:03:34.904: W/System.err(770): java.text.ParseException: Unparseable date: Oct 21, 2013 12:03:34 AM
10-21 00:03:34.904: W/System.err(770):  at java.text.DateFormat.parse(DateFormat.java:645)
10-21 00:03:34.914: W/System.err(770):  at com.example.datesdifference.MainActivity.showDifference(MainActivity.java:60)
10-21 00:03:34.914: W/System.err(770):  at com.example.datesdifference.MainActivity.onCreate(MainActivity.java:25)
10-21 00:03:34.914: W/System.err(770):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-21 00:03:34.914: W/System.err(770):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-21 00:03:34.914: W/System.err(770):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-21 00:03:34.914: W/System.err(770):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-21 00:03:34.914: W/System.err(770):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-21 00:03:34.914: W/System.err(770):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-21 00:03:34.914: W/System.err(770):  at android.os.Looper.loop(Looper.java:123)
10-21 00:03:34.914: W/System.err(770):  at android.app.ActivityThread.main(ActivityThread.java:4627)

I have searched this on google but unable to resolve this. Can anybody help please ?

user2011302
  • 391
  • 1
  • 4
  • 22
  • Why are you converting a date to a string, then parsing it back? There is a simple rule in programming. Only convert a date to a string when you want to show it to the user, store it somewhere that only stores strings or when passing it to another app. – Simon Oct 20 '13 at 19:54
  • Actually the act_dateString is the date stored in the Sqlite database and returned in cursor. – user2011302 Oct 20 '13 at 20:02

5 Answers5

0

Probably the output of toLocaleString does not match the format you use in formatter1, so a exception is thrown.

You should try to see what act_dateString looks like, then you know what goes wrong.

Peterdk
  • 15,625
  • 20
  • 101
  • 140
0

If you want to convert calendar to Date then you can use this i believe

Date date_old =  new Date(cal.getTimeInMillis());
nandeesh
  • 24,740
  • 6
  • 69
  • 79
0
    String act_dateString;
    SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy/MM/dd");
    Calendar cal =  Calendar.getInstance();
    Date date_old = null;
    act_dateString =formatter1.format(cal.getTime());
       try {
           date_old = formatter1.parse(act_dateString);
       } catch (ParseException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
0

You have to do this way to parse the date. For example,In your code, calling toLocalString() will give you the String 'Oct 20, 2013 12:57:28 AM'. This will throw java.text.ParseException: Unparseable date: "Oct 20, 2013 12:57:28 AM You have to input a Date String with the format "yyyy/MM/dd". For that you have to convert the date format to the String format "yyyy/MM/dd". then, it will be able to parse the input String

  String DATE_FORMAT_NOW = "yyyy/MM/dd";
    Calendar cal = Calendar.getInstance();
    Date date = null;
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
    String dateInStringFormat = sdf.format(cal.getTime());
    try {
        //parsing the date string with format "yyyy/MM/dd"
        date = sdf.parse(dateInStringFormat);
        System.out.println(date.toLocaleString());
    } catch (ParseException ex) {
        Logger.getLogger(DateTest.class.getName()).log(Level.SEVERE, null, ex);
    }

Hope you understand why the exception is thrown now. To find the difference between two dates in days,

   long diffInDays = (newerDate.getTime() - olderDate.getTime())/(1000*60*60*24);
   System.out.println(diffInDays);
Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
0

You are using troublesome old legacy date-time classes now supplanted by the java.time classes.

Your input string is almost in standard ISO 8601 format. Replace the slash characters with hyphen characters.

LocalDate ld = LocalDate.parse( "2016/01/02".replace( "/" , "-" ) ) ;

Determining today’s date requires a time zone as the date varies around the globe by zone at any given moment.

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );

The ThreeTen-Extra project extends java.time with additional classes. The Days class represents a span of time as days.

int days = Days.between( ld , today ).getAmount() ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154