只需要日历实例的日期(而不是时间),并在滞后时与Kotlin中的日期字符串进行比较 [英] Need Calendar Instance only date (not time) and compare with date String in Kotlin as it lags
问题描述
我只需要日历实例中的日期,而不需要时间.每当我使用日历对象时,它都会返回带有时间的日期.
I need only date from Calendar Instance not the time. Whenever i used calendar object it returns the date with time.
val calendar = Calendar.getInstance()
calendar.time. // Mon Nov 09 11:41:29 GMT 2020
我通过使用SimpleDateFormat对此进行了更改
I change this by using SimpleDateFormat
SimpleDateFormat("dd/MM/yyyy").format(date)
09/09/2020
我正在创建日历,因此列表中有大量数据.我要在特定日期添加数据.因此,我正在将日期与字符串日期进行比较.我的字符串日期格式如下所示:-
I am creating calendar so i have huge amount of data in list. I am adding data at specific date. So I am comparing dates with string date. My string date Format is look like this :-
20/05/2020
因此存在太多的性能问题,例如滞后于视图.因此,有什么我可以避免的事情.
So there is too much performance issue like lagging the view. So is there any thing which i can use to avoid all this thing.
val calendarModel = dataList?.find {
SimpleDateFormat("dd/MM/yyyy").format(it.date) == item
}
推荐答案
Calendar#getTime
returns a java.util.Date
object representing this Calendar's time value which is a millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT
.
因此,java.util.Date
不代表实际日期或时间或日期时间对象.当您打印此毫秒值时,JVM将在其时区中计算日期和时间,并且在打印其对象时,您将得到java.util.Date#toString
返回的内容.从这个解释中,您必须已经了解到,该毫秒值与时区无关,因为它不是基于时区的值,所以它将是相同的.相反,它被java.util.Date#toString
假表示为基于时区的值.只是为了演示我刚才所说的内容,请看以下程序的输出:
Thus, java.util.Date
does not represent a real date or time or date-time object. When you print this millisecond value, your JVM calculates the date and time in its time-zone and when you print its object, you get what java.util.Date#toString
returns. From this explanation, you must have already understood that this millisecond value will be the same irrespective of the timezone as it is not a timezone based value; rather, it is fakely represented by java.util.Date#toString
as a timezone based value. Just to demonstrate what I have just said, look at the output of the following program:
import java.util.Date;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) {
Date date = new Date();
System.out.println("Asia/Calcutta:");
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Calcutta"));
System.out.println(date.getTime());
System.out.println(date);
System.out.println("\nEurope/London:");
TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
System.out.println(date.getTime());
System.out.println(date);
System.out.println("\nAfrica/Johannesburg:");
TimeZone.setDefault(TimeZone.getTimeZone("Africa/Johannesburg"));
System.out.println(date.getTime());
System.out.println(date);
System.out.println("\nAmerica/New_York:");
TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"));
System.out.println(date.getTime());
System.out.println(date);
}
}
输出:
Asia/Calcutta:
1604747702688
Sat Nov 07 16:45:02 IST 2020
Europe/London:
1604747702688
Sat Nov 07 11:15:02 GMT 2020
Africa/Johannesburg:
1604747702688
Sat Nov 07 13:15:02 SAST 2020
America/New_York:
1604747702688
Sat Nov 07 06:15:02 EST 2020
现代日期时间API 具有真实的日期时间类.下面给出的是这些类的概述:
The modern date-time API has real date-time classes. Given below is an overview of these classes:
您可以在此表中找到一个类LocalDate
,该类仅表示日期(由年,月和日组成).以下是现代java.time
API的快速演示:
As you can find in this table, there is a class, LocalDate
which represents just date (consisting of a year, month, and day). Given below is a quick demo of the modern java.time
API:
import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZoneOffset;
public class Main {
public static void main(String[] args) {
// A date with the given year, month and day-of-month
LocalDate date = LocalDate.of(2010, Month.NOVEMBER, 7);
System.out.println(date);
// Today (in the JVM's timezone)
LocalDate today = LocalDate.now(); // Same as LocalDate.now(ZoneId.systemDefault())
System.out.println(today);
// Today at UTC
LocalDate todayAtUTC = LocalDate.now(ZoneOffset.UTC);
System.out.println(todayAtUTC);
// Today in India
LocalDate todayInIndia = LocalDate.now(ZoneId.of("Asia/Calcutta"));
System.out.println(todayAtUTC);
}
}
输出:
2010-11-07
2020-11-07
2020-11-07
2020-11-07
在 跟踪中了解有关现代日期时间API的更多信息:日期时间 .
Learn more about the modern date-time API at Trail: Date Time.
建议:java.util
的日期时间API及其格式API SimpleDateFormat
已过时且容易出错.我建议您应该完全停止使用它们,并切换到现代日期时间API .
Recommendation: The date-time API of java.util
and their formatting API, SimpleDateFormat
are outdated and error-prone. I suggest you should stop using them completely and switch to the modern date-time API.
如果您正在为Android项目工作,并且您的Android API级别仍不符合Java-8,请检查如何在Android Project中使用ThreeTenABP .
If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
这篇关于只需要日历实例的日期(而不是时间),并在滞后时与Kotlin中的日期字符串进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!