javaandroiddatekotlincalendar

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

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
}

Solution

  • 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.

    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);
        }
    }
    

    Output:

    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
    

    The modern date-time API has real date-time classes. Given below is an overview of these classes:

    enter image description here

    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);
        }
    }
    

    Output:

    2010-11-07
    2020-11-07
    2020-11-07
    2020-11-07
    

    Learn more about the modern date-time API at Trail: Date Time.

    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.

    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.