1

Hello what I am trying to do is compare a date I have in specific format(2020-02-27T15:00:43+0000 format) and today's date.

Just the date not the time.

String saleDate=2020-02-27T15:00:43+0000;

            public static int validateCustomer(int idCustomer, double saleBalance, double totalSaleValue, String saleDate){
    Config config= new Config().getConfig();
    ModuleRoute moduleRoute = new ModuleRoute().getModulesRoute(config.idRoute);
    double limit = (double) moduleRoute.allowPercentage;
    double paid =totalSaleValue-saleBalance;
    Date saleDateFormatD = null;
    Date dateToday = new Date();
    SimpleDateFormat saleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    try {
        saleDateFormatD = saleDateFormat.parse(saleDate);
    } catch (java.text.ParseException e) {
        e.printStackTrace();
    }
    SimpleDateFormat justDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    String formatSaleDateTime = justDateFormat.format(saleDateFormatD);
    String todayDate= justDateFormat.format(dateToday);
    if (formatSaleDateTime.equals(todayDate)){
        return 0;
    }else if ((paid)>(limit)*totalSaleValue){
        return 1;
    }else
        return 2;
    //if customer can, return true


}

In this case, it will be check if dt and date1 are the same.

date is a string in that format. Any help or suggestions would be great. thanks

4

2 回答 2

1

Try this, hope it may help you

    //Declare it in your class.   
private static final DateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd");      //Changed here
    //just set your saleDate as
String saleDate = "2020-02-27"; //now both are in same format

Date newDate = new Date();
String newDateFormat = sdf.format(newDate);
Log.d(TAG, "New Date: " + newDateFormat ); // It gave me output as - D/TAG: New Date: 2020-02-28
if (newDateFormat.equalsIgnoreCase(saleDate))  // make your static date as of same format ("yyyy-MM-dd")
{
      //Date is matched
}

You will get your current time in the format you specified.

Now you can compare both strings(newDateFormat & date) easily.(Both are in same format).

Perform just simple string equals on both strings.

于 2020-02-27T16:55:07.877 回答
0

Here I have a generic function that works for almost everthing (keeping the Daylight saving time):

function calcDiff2Dates(llegada, salida) {
    //DESMENUZAMOS ESE STRING
    var salidaParts = salida.split("/");
    var llegadaParts = llegada.split("/");

    // los meses empiezan en 0, de ahí -> dataParts[1] - 1
    var salidaDateObject = new Date(+salidaParts[2], salidaParts[1] - 1, +salidaParts[0]);
    var llegadaDateObject = new Date(+llegadaParts[2], llegadaParts[1] - 1, +llegadaParts[0]);

    //RECOGEMOS EL VALOR DE UN DIA PARA OPERAR
    const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
    const diffDays = Math.round(Math.abs((llegadaDateObject - salidaDateObject) / oneDay));

    //PASAMOS VALOR DEL CAMPO:
    //guardamos en var el htlm field que queremos popular
    var diasFiltroTop = $('#totalDiasFiltroTop');
    diasFiltroTop.html(diffDays);
};

Pass as a variable and should be ready:

var salida = document.getElementById('fechaArrivalFiltroTop').innerHTML;
var llegada = document.getElementById('fechaDepartureFiltroTop').innerHTML;

    calcDiff2Dates(llegada, salida);

In your case if its return 0 then its the same day.

于 2020-02-27T16:40:55.353 回答