3

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.

Ahmed Zafar
  • 665
  • 2
  • 10
  • 24

2 Answers2

3

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?

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

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

Use Calendar.

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

You can also consider Time class.

Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
  • Thanks. This is java.util. I hope that doesn't make a difference? – Ahmed Zafar Aug 23 '13 at 10:33
  • @AhmedZafar No problem. Calendar is great for the job in your question. – Sajal Dutta Aug 23 '13 at 10:37
  • Thanks this was a real concern. – Ahmed Zafar Aug 23 '13 at 10:39
  • @AhmedZafar Note that if your app has a great deal to do with the device time, then user can cheat away with the system by manually changing the time no matter what class you use. In that case, you have to connect to internet and may use a public time server (You do NOT need to set up your own server). You can see my answer here about time server: http://stackoverflow.com/questions/17907418/real-time-game-development/17907560 – Sajal Dutta Aug 23 '13 at 10:50
  • Thanks a lot Sajal. I know how to get time from the internet. I don't expect the cheating because the users, which are a select group, will know that the time is fetched from the phone. Thank you for your help. One other question if you know: Can you guide me on how to authenticate an app so that it can only be used to a few individuals? – Ahmed Zafar Aug 23 '13 at 12:58
  • @AhmedZafar No problem. This might help you as I didn't do much authentication: http://stackoverflow.com/questions/8954569/how-to-put-user-authentication-into-a-mobile-application – Sajal Dutta Aug 23 '13 at 13:19