0

im developing a time management app. For my assignment module part, i allow the user choose the due date they want, and it will compare with the phone time and it will highlight the assignment tittle become red color before one day the assignment due date.

UPDATED PROBLEM im successful get both date and compare bt get error remainder of day. below is my source code, sry for my coding flow, i know its very mess.

    // get the current date
    final Calendar c = Calendar.getInstance();
    Date today = Calendar.getInstance().getTime();
    //show current date
    TextView current = (TextView)findViewById(R.id.textView);
    SimpleDateFormat df = new SimpleDateFormat("ddMyyyy");
    String formattedDate = df.format(c.getTime());
    current.setText(formattedDate);

    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);
    //get the remainder date
    int days = Days.daysBetween(new DateTime(formattedDate), new DateTime(today)).getDays();

    //show the remainder date
    String tempdays = String.valueOf(days);
    TextView txtdate3 = (TextView) findViewById(R.id.textView3);
    txtdate3.setText(tempdays);

     //show the user input date from datepicker
    label=(TextView)findViewById(R.id.textView2);
    // display the current date
    updatedate();

    btn=(Button)findViewById(R.id.button);
    btn.setOnClickListener(this);

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{my.com.chyi.assigment/my.com.chyi.assigment.MainActivity}: java.lang.IllegalArgumentException: Invalid format: "06/22/2010" is malformed at "/22/2010"java.lang.IllegalArgumentException: Invalid format: "20/20/2015" is malformed at "/20/2015" java.lang.IllegalArgumentException: Invalid format: "21/20/2015" is malformed at "/20/2015" java.lang.ArithmeticException: Value cannot fit in an int: -7743140921java.lang.ArithmeticException: Value cannot fit in an int: -9204110890java.lang.ArithmeticException: Value cannot fit in an int: -7396160515java.lang.ArithmeticException: Value cannot fit in an int: -6355219424java.lang.ArithmeticException: Value cannot fit in an int: -6388091249

Wei Chi
  • 29
  • 1
  • 8

1 Answers1

0

If you use the JodaTime library you could simply write it like that:

import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.Days;

Date past = new Date(110, 5, 20); // June 20th, 2010
Date today = new Date(110, 6, 24); // July 24th 
int days = Days.daysBetween(new DateTime(past), new DateTime(today)).getDays(); // => 34

Difference in days between two dates in Java?

Community
  • 1
  • 1
Juce
  • 341
  • 1
  • 8
  • no prob ;3 hope thats the functionality that you wanted ^^ – Juce Mar 16 '15 at 09:05
  • sry,if i want to compare user input date and computer date, and i having this two variable(strdate2 + label ) to store the date from user input and computer date, which part should i modify in order to get the different day between this two date? – Wei Chi Mar 16 '15 at 09:43
  • You dont need them as String you need them as Date objects. Then you just put the 2 DateObjects in `int days = Days.daysBetween(new DateTime(date 1), new DateTime(date 2)).getDays(); // => 34` – Juce Mar 16 '15 at 10:04
  • i tried already but get 0 result, i have replace it like int days = Days.daysBetween(new DateTime(c2), new DateTime(calendar.getTime())).getDays(); Calendar c2 = Calendar.getInstance(); calendar.getTime() is datepickerDialog – Wei Chi Mar 16 '15 at 10:42
  • im successful compare if the date 1 is preset date, hw if i want the date1 is get from datepickerdialog date? – Wei Chi Mar 16 '15 at 11:24
  • just get it from the datepickerdialog and cast it into date form? – Juce Mar 16 '15 at 14:05
  • i cast the calendar to the date2 but fail `public void setDate(){ new DatePickerDialog(MainActivity.this,d,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show(); }` – Wei Chi Mar 16 '15 at 14:12
  • WTF are you doing >_< you just need to call `c2.getTime()` or you use the `getTimeInMillis();` – Juce Mar 16 '15 at 14:38
  • i can get the c2 time, bt i dont how to know get the datepicker time. – Wei Chi Mar 16 '15 at 14:42
  • I dont know which datepicker you use so just look in the api of the datepicker. there should be a method for that – Juce Mar 16 '15 at 14:47
  • i get error of remainder day, both of my computer date and calendar format is correct. do you know what problem is that ? – Wei Chi Mar 17 '15 at 08:46
  • pls post the stacktrace ^^ or even the important line of it what exception is it – Juce Mar 17 '15 at 10:10
  • my.com.chyi.assigment.MainActivity.onCreate(MainActivity.java:97)-> all has point to a blank line, and i already post out the stacktrace at top – Wei Chi Mar 17 '15 at 10:33
  • ... please read your own stacktrace ... it says it can't handle the format of your date. – Juce Mar 17 '15 at 12:58
  • omg...im successful get the remainder day :DDDDD, is my current date format problem. thx for your help – Wei Chi Mar 17 '15 at 15:13