Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

time vs date vs calendar java

Well in my app I want to get current date and month abnd local hour (currentTimeZone hour) and depending on its value display some things.

i have read several sollutions and I have used the following:

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

or this

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

my questions are:

1)which of this is more "Stable" considering their results? I mean that it will not have any problem.

2)Moreover, did they both take into consideration the TimeZone? I think that the second one does, but what about the calendar?

3)I tried this code that I have found:

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

and i get the error that Date() must have an argument. weird because in every answer they use it like that.

What is wrong?

4) What do you suggest me to use? The only thing I want is a simple comparison between current month,date,hour with some stored in memory. (for example if its January 29, display that event. if it is 19.00 am send notification and so on.)

like image 569
ghostrider Avatar asked Sep 08 '12 22:09

ghostrider


People also ask

What is the difference between calendar and date in Java?

What is difference between Java Date and Calendar classes? The difference between Date and Calendar is that Date class operates with specific instant in time and Calendar operates with difference between two dates.

Is Java calendar deprecated?

Date are deprecated since Java 1.1, the class itself (and java. util. Calendar , too) are not officially deprecated, just declared as de facto legacy. The support of the old classes is still important for the goal of backwards compatibility with legacy code.

Is calendar immutable in Java?

Date and Time APIs in Java 8 are immutable and therefore thread safe.

What is calendar date in Java?

Calendar class in Java is an abstract class that provides methods for converting date between a specific instant in time and a set of calendar fields such as MONTH, YEAR, HOUR, etc. It inherits Object class and implements the Comparable, Serializable, Cloneable interfaces.


2 Answers

1) Which of this is more "Stable" considering their results? I mean that it will not have any problem.

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

An alternative to the Calendar and GregorianCalendar classes. An instance of the Time class represents a moment in time, specified with second precision. It is modelled after struct tm, and in fact, uses struct tm to implement most of the functionality.

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

2) Moreover, did they both take into consideration the TimeZone? I think that the second one does, but what about the calendar?

Yes, they do. You can specify a TimeZone using Calendar.getInstance(java.util.TimeZone). Otherwise, a default one will be used.

3) I tried this code that I have found:

The code works just fine. Make sure you are using 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 a simple comparison between current month,date,hour with some stored in memory. (for example if its January 29, display that event. if it is 19.00 am send notification and so on.)

It's really a matter of personal choice. Using Calendar, for example, to compare if today is January 29, you could 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
}
like image 109
João Silva Avatar answered Oct 14 '22 13:10

João Silva


tl;dr

ZonedDateTime.now().getDayOfMonth()

java.time

As others commented, you appear to be using and mixing up java.sql.Time, java.sql.Date, java.util.Date, and java.util.Calendar.

All of those troublesome classes are now legacy, supplanted by java.time classes added to Java 8 and later. For earlier Android, see last bullets below.

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

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.

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

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

Pass the zone when asking for the current moment.

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

Interrogate for the parts you desire.

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 supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

like image 45
Basil Bourque Avatar answered Oct 14 '22 11:10

Basil Bourque