nun*_*nos 89 java datetime android
我试图将String格式的日期与当前日期进行比较.这是我做的方式(没有测试,但应该工作),但我使用弃用的方法.对替代方案有什么好的建议吗?谢谢.
PS我真的很讨厌用Java做Date的东西.有很多方法可以做同样的事情,你真的不确定哪一个是正确的,所以我的问题在这里.
String valid_until = "1/1/1990";
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
Date strDate = sdf.parse(valid_until);
int year = strDate.getYear(); // this is deprecated
int month = strDate.getMonth() // this is deprecated
int day = strDate.getDay(); // this is deprecated
Calendar validDate = Calendar.getInstance();
validDate.set(year, month, day);
Calendar currentDate = Calendar.getInstance();
if (currentDate.after(validDate)) {
catalog_outdated = 1;
}
Run Code Online (Sandbox Code Playgroud)
JB *_*zet 195
您的代码可以缩减为
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date strDate = sdf.parse(valid_until);
if (new Date().after(strDate)) {
catalog_outdated = 1;
}
Run Code Online (Sandbox Code Playgroud)
要么
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date strDate = sdf.parse(valid_until);
if (System.currentTimeMillis() > strDate.getTime()) {
catalog_outdated = 1;
}
Run Code Online (Sandbox Code Playgroud)
Int*_*iya 20
你可以使用compareTo()
如果当前对象小于其他对象,则CompareTo方法必须返回负数,如果当前对象大于其他对象,则返回正数;如果两个对象彼此相等,则返回零.
// Get Current Date Time
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm aa");
String getCurrentDateTime = sdf.format(c.getTime());
String getMyTime="05/19/2016 09:45 PM ";
Log.d("getCurrentDateTime",getCurrentDateTime);
// getCurrentDateTime: 05/23/2016 18:49 PM
if (getCurrentDateTime.compareTo(getMyTime) < 0)
{
}
else
{
Log.d("Return","getMyTime older than getCurrentDateTime ");
}
Run Code Online (Sandbox Code Playgroud)
您可以直接创建Calendar一个Date:
Calendar validDate = new GregorianCalendar();
validDate.setTime(strDate);
if (Calendar.getInstance().after(validDate)) {
catalog_outdated = 1;
}
Run Code Online (Sandbox Code Playgroud)
请注意,在代码工作之前,正确的格式是("dd/MM/yyyy")."mm"意味着小吃!
String valid_until = "01/07/2013";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date strDate = null;
try {
strDate = sdf.parse(valid_until);
} catch (ParseException e) {
e.printStackTrace();
}
if (new Date().after(strDate)) {
catalog_outdated = 1;
}
Run Code Online (Sandbox Code Playgroud)
Calendar toDayCalendar = Calendar.getInstance();
Date date1 = toDayCalendar.getTime();
Calendar tomorrowCalendar = Calendar.getInstance();
tomorrowCalendar.add(Calendar.DAY_OF_MONTH,1);
Date date2 = tomorrowCalendar.getTime();
// date1 is a present date and date2 is tomorrow date
if ( date1.compareTo(date2) < 0 ) {
// 0 comes when two date are same,
// 1 comes when date1 is higher then date2
// -1 comes when date1 is lower then date2
}
Run Code Online (Sandbox Code Playgroud)
String date = "03/26/2012 11:00:00";
String dateafter = "03/26/2012 11:59:00";
SimpleDateFormat dateFormat = new SimpleDateFormat(
"MM/dd/yyyy hh:mm:ss");
Date convertedDate = new Date();
Date convertedDate2 = new Date();
try {
convertedDate = dateFormat.parse(date);
convertedDate2 = dateFormat.parse(dateafter);
if (convertedDate2.after(convertedDate)) {
txtView.setText("true");
} else {
txtView.setText("false");
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
它返回true ..并且您还可以在date.before和date.equal的帮助下检查before和equal。
将日期转换为日历并在那里进行计算。:)
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int year = cal.get(Calendar.YEAR);
int month = cal.geT(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH); //same as cal.get(Calendar.DATE)
Run Code Online (Sandbox Code Playgroud)
或者:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date strDate = sdf.parse(valid_until);
if (strDate.after(new Date()) {
catalog_outdated = 1;
}
Run Code Online (Sandbox Code Playgroud)
在 Kotlin 中,使用内置函数 after() 或 before() 来比较两个时间对象非常简单:
expirationTime.after(Date())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
152549 次 |
| 最近记录: |