Time vs date vs java calendar

In my application, I want to get the current date and month abnd local hour (currentTimeZone hour), and depending on its value, some things will be displayed.

I read some solutions and I used the following:

Calendar c = Calendar.getInstance(); int day=c.get(Calendar.DATE); int month=c.get(Calendar.MONTH); 

or

 Time today = new Time(Time.getCurrentTimezone()); today.setToNow(); int day2=today.monthDay; int month2=today.month; 

my questions:

1) which of these is more "stable", given their results? I mean, he will not have any problems.

2) In addition, did they both take into account TimeZone? I think the second one does, but what about the calendar?

3) I tried this code, which I found:

 SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss"); String format = s.format(new Date()); 

and I get an error that should have a Date () argument. strange because in every answer they use it like that.

What's wrong?

4) What do you suggest me to use? The only thing I want is a simple comparison between the current month, date, hour with some stored in memory. (for example, if it displays this event on January 29, if it was sent 19.00, etc.)

+4
source share
4 answers

1) Which one is more "stable", given their results? I mean that he will not have any problems.

They are stable and very well tested, especially Calendar , which are in the Java API with JDK1.1. Also from docs Time :

An alternative to the Calendar and GregorianCalendar classes. an instance of the Time class represents the point in time specified by the second precision. It is modeled after struct tm and actually uses struct tm to implement most functions.

Time , imho, is much easier to work than Calendar .

2) In addition, did they both take into account TimeZone? I think the second one does, but what about the calendar?

Yes Yes. You can specify TimeZone using Calendar.getInstance(java.util.TimeZone) . Otherwise, the default value will be used.

3) I tried this code, which I found:

The code is working fine. Make sure you use new java.util.Date() instead of java.sql.Date , for example.

4) What do you suggest me to use? The only thing I want is just a comparison of the current month, date, hour with some stored in memory. (for example, if its January 29, display this event, if it takes place 19.00 send notifications, etc.)

This is really a matter of personal choice. For example, using Calendar to compare if today is January 29th, you can simply use the following:

 Calendar today = Calendar.getInstance(); int dayOfMonth = today.get(Calendar.DAY_OF_MONTH); int month = today.get(Calendar.MONTH); if (month == Calendar.JANUARY && dayOfMonth == 29) { // January 29 } 
+12
source

In my Android app, I save the date in SQLite. However, SQLite does not accept the Date data type. Therefore, I convert it to String so that I use the format YYYY-MM-DD :

  Time today = new Time(Time.getCurrentTimezone()); today.setToNow(); int year = today.year; int month = today.month; int day = today.monthDay; Calendar gregorianCalendar = new GregorianCalendar(year, month, day); String entryDate = gregorianCalendar.get(Calendar.YEAR) + "-" + gregorianCalendar.get(Calendar.MONTH) + "-" + gregorianCalendar.get(Calendar.DAY_OF_MONTH); 

And it works well. I'm not interested in performance differences, as they are probably careless.

0
source

android.text.format.Time is deprecated at API level 22. Instead, use GregorianCalendar.

Visit http://developer.android.com/reference/android/text/format/Time.html

As I know, there are some problems in the time class, such as Time.setJulianDay (int julianDay) will return the wrong value when the argument is less than julianDay from 1970.1.1

And some errors also happen in April 1987-1991 (maybe this is Time.set (long millis), I don’t remember).

0
source

TL; DR

 ZonedDateTime.now().getDayOfMonth() 

java.time

As others have commented, you seem to use and mix java.sql.Time , java.sql.Date , java.util.Date and java.util.Calendar .

All these nasty classes are now deprecated , superseded by java.time classes added in Java 8 and later. For earlier Android, see Recent Bullets below.

To get the current moment in a specific time zone, use ZonedDateTime .

The time zone is critical for determining the date. At any given moment, the date changes around the world by zone. For example, a few minutes after midnight in Paris, France is a new day, still "yesterday" in Montreal Quebec .

If no time zone is specified, the JVM implicitly applies the current default time zone. This default value may change at any time, so your results may vary. It is better to specify the desired / expected time zone explicitly as an argument.

Specify the time zone name in continent/region format, such as America/Montreal , Africa/Casablanca or Pacific/Auckland . Never use the abbreviation 3-4 letters, for example EST or IST , as they are not real time zones, and are not standardized and not even unique (!).

 ZoneId z = ZoneId.of( "America/Montreal" ) ; 

Skip the zone when asking for the current moment.

 ZonedDateTime zdt = ZonedDateTime.now( z ) ; // Current moment as seen by the people of particular region. 

Interview the desired parts.

 Month month = zdt.getMonth() ; // Get `Month` enum object. int monthNumber = zdt.getMonthValue() ; // Get month number, 1-12 for January-December. int dayOfMonth = zdt.getDayOfMonth() ; 

About java.time

The java.time framework is built into Java 8 and later. These classes will be replaced by the troublesome old legacy date and time classes, such as java.util.Date , Calendar and SimpleDateFormat .

The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.

To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .

Where to get java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. You can find some useful classes here, such as Interval , YearWeek , YearQuarter and more .

0
source

Source: https://habr.com/ru/post/1433175/


All Articles