首页 > 解决方案 > How to get date from 2020-02-27T15:00:43+0000 format

问题描述

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

标签: androidformatdatetime

解决方案


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.


推荐阅读