【问题标题】:Parse String to Date and calculate diference between on Two dates [duplicate]将字符串解析为日期并计算两个日期之间的差异[重复]
【发布时间】:2013-12-21 04:58:33
【问题描述】:

嗯,我在 Android 上工作,我从 JSon 获得了一个字符串格式的日期,格式为:“dd-MM-yyyy”,今天从系统获得日期,所以我无法得到两个日期之间的差异。我做到了。

SimpleDateFormat df1 = new SimpleDateFormat("dd-MM-yyyy",java.util.Locale.getDefault());
String fechaActual = df1.format(c.getTime());
System.out.println("=========> Date Today => "+fechaActual); 

fechaEvaluacion = json_data.getString("fechaEvaluacion");
Date fechaEvDate = Date.valueOf(fechaEvaluacion);
long fechaInicial = fechaEvDate.getTime();
long fechaFinal = xDate.getTime(); 
long diferencia =  ( fechaFinal- fechaInicial ); 
final long days = diferencia / 24;
System.out.println("The difference between the two days is: "+days);
if(days < 3){
    runOnUiThread(new Runnable() {
        public void run() {
            Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.vibrate(200);
            Toast toast2 = Toast.makeText(getApplicationContext(), "Tienes "+tipoEvaluacion+" de: "+nombreAsignatura+" en "+days+", el día:  "+fechaEvaluacion, Toast.LENGTH_LONG);
            toast2.setGravity(Gravity.TOP|Gravity.RIGHT, 0, 0);
            toast2.show();
        }
    });
}

最后在可变的“天数”中,我得到了一些数字,例如 -13140000000 或 13176000000

我需要一些帮助,我尝试了很多方法来解析日期,但没有结果。

【问题讨论】:

    标签: android string date


    【解决方案1】:

    你应该这样划分

    final long days = diferencia / (1000 * 60 * 60 * 24);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-02
      • 2019-08-24
      • 1970-01-01
      • 2014-08-23
      相关资源
      最近更新 更多