0

my textview need to display date of exam dead line. My json passes it as a string . Then i need to get current date and display the remain date on another textviwe

String date    ="2014-11-12";  //ok
Date deadline  = //how to cast string to date  
//then how to get today and subtract 
textview1.setText(today);      
int remaindays = //how to get it    
textview2.setText("You have " +remaindays  +" days to exam");
  • split the string date by "-", then create a new date with year, month and day – mlwn Nov 18 '14 at 18:10
  • possible duplicate of [Best way to compare dates in Android](http://stackoverflow.com/questions/10774871/best-way-to-compare-dates-in-android) – Semih Eker Nov 18 '14 at 18:10
  • Please search StackOverflow before posting. Your Question’s topics have been addressed many hundreds of times in previous Questions and Answers. Such as [this](http://stackoverflow.com/q/4216745/642706). Tip: try searching for "joda" (yes, spelled correctly). – Basil Bourque Nov 18 '14 at 19:07

2 Answers2

0

Try this code

 String date    ="2014-11-12"; 
 Date deadline; 
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
 Date strDate = sdf.parse(date);
 Long diff=-1;
 int diff=0;
 if (System.currentTimeMillis() > strDate.getTime()) {
    diff=System.currentTimeMillis()-strDate.getTime();
 }
 int remaindays = (int)diff/100/60/60/24
 textview2.setText("You have " +remaindays  +" days to exam");
Darish
  • 11,032
  • 5
  • 50
  • 70
0

1. Cast the string to date

 String date = "2014-11-12";  
        SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd");  
        try {  
            Date date = format.parse(date);  
            System.out.println(date);  
        } catch (ParseException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
    }

2. get today date

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String currentDateandTime = sdf.format(new Date());

3. sustraction of date

long diff = date2.getTime() - date1.getTime();
int remaindays = (int)diff/100/60/60/24

Does this make sense to you?

Darish
  • 11,032
  • 5
  • 50
  • 70
Li Chao
  • 326
  • 2
  • 7