android - 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
解决方案
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.
推荐阅读
- r - 将数据表行元素组合成一个新列作为向量
- javascript - 如何使用 fetch 来获取和显示信息
- python - 如何在 Python(Jupyter)中构建一个函数来计算点扩散函数(图像处理?)
- php - Laravel 7 - 帮助添加/更新
- python - python - 如何根据python中pandas数据框中的列并按降序分组?(Jupyter 笔记本)
- ruby-on-rails - Excon::Error::Forbidden: Expected(200) <=> Actual(403 Forbidden) with rails app
- reactjs - 卸载具有反应功能的组件
- javascript - Angular 9:如何使用带有查询字符串的 route.navigate() 在新选项卡中打开 URL?
- amazon-s3 - Kafka Connect S3 Sink Flush 数据 - 奇怪的延迟
- firebase - 使用 arrayContainsAny 进行 Flutter Firestore 查询:列表不起作用