Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java library or Android for get time and date

Simple noob question: Should I use java.util to get system date and time for an android app or should I use the android calender class. Java is much more easier for me to use as it makes getting the day of the week that much more easier for me. Please guide.

Thanks

EDIT: I want the app to function without an internet connection, getting the time and day from the phone.

like image 898
Ahmed Zafar Avatar asked Mar 03 '26 14:03

Ahmed Zafar


2 Answers

tl;dr

LocalDate                        // Represent a date, without time-of-day, and without time zone.
.now( ZoneId.systemDefault() )   // Capture the current date as seen in a particular time zone.
.getDayOfWeek()                  // Returns a `DayOfWeek` enum object.
.getDisplayName​(                 // Generate localized text for the day-of-week’s name.
    TextStyle.FULL ,             // How long or abbreviated should be the day-of-week name.
    Locale.getDefault()          // The human language and cultural norms to use in localizing.
)

Monday

java.time

Should I use java.util to get system date and time for an android app or should I use the android calender class

Neither. The modern solution is java.time classes.

You asked:

getting the day of the week

Capture the current date as seen in the JVM’s current default time zone.

LocalDate ld = LocalDate.now( ZoneId.systemDefault() ) ;

Extract the day of week.

DayOfWeek dow = ld.getDayOfWeek() ;

Generate localized text for the name of the day of the week.

Locale locale = Locale.getDefault() ;
String output = dow.getDisplayName​( TextStyle.FULL , locale ) ;

Table of all date-time types in Java, both modern and legacy


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.

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

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

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
    • Java 9 brought some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android (26+) bundle implementations of the java.time classes.
    • For earlier Android (<26), a process known as API desugaring brings a subset of the java.time functionality not originally built into Android.
      • If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….

Table of which java.time library to use with which version of Java or Android

like image 164
Basil Bourque Avatar answered Mar 05 '26 02:03

Basil Bourque


Use Calendar.

Calendar cal = Calendar.getInstance(); 
int week = cal.get(Calendar.DAY_OF_WEEK);

You can also consider Time class.

like image 28
Sajal Dutta Avatar answered Mar 05 '26 04:03

Sajal Dutta