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.)
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.
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.
Date and Time APIs in Java 8 are immutable and therefore thread safe.
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.
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
}
ZonedDateTime.now().getDayOfMonth()
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() ;
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With