0

I am building a simple age calculator application using Kotlin.

public fun calculateage(view: View){
    val dob: String = etbirth.text.toString()
    val currentyear = calendar.getInstance().get(Calendar.Year)
    val age = currentyear - dob.toInt()
    tvage.setText("$age")
}

This method is not available in API level 15, requiring API level 24 minimum.

So how can I get the current year?

Filipe Freire
  • 823
  • 7
  • 21
s.nainwal
  • 21
  • 5
  • 1
    `SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); String currentyear = sdf.format(new Date());` – Jyoti JK Mar 24 '18 at 10:32
  • Calendar class is available from API 1. Which exactly method requires API 24? – Vladyslav Matviienko Mar 24 '18 at 11:03
  • 1
    Also why is the question tagged with `java` while it is in `kotlin`? – Vladyslav Matviienko Mar 24 '18 at 11:03
  • jyoti jk---->thank you its working – s.nainwal Mar 24 '18 at 11:15
  • Vladyslav mantviienko--->this method requires apli level 24 or above – s.nainwal Mar 24 '18 at 11:16
  • Vladyslav mantviienko--->it is tagged in java because it uses java.util.* for using SimpleDateFormat – s.nainwal Mar 24 '18 at 11:17
  • 1
    FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. Much of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Mar 24 '18 at 19:57

3 Answers3

5
val year = Calendar.getInstance().get(Calendar.YEAR)

I tested this code, it works well even the min api level is 15.

enter image description here

Is this the picture you want?

L. Swifter
  • 3,179
  • 28
  • 52
1

tl;dr

org.threeten.bp.Year.parse(          // Parse a string into a `Year` object. Using the back-port of *java.time* classes found in the *ThreeTen-Backport* project, further adapted for earlier Android in the *ThreeTenABP* project.
    "1961"                           // String input representing a year.
)
.until(                              // Calculate elapsed time.
    Year.now(                        // Get the current date’s year as a `Year` object rather than a mere integer number.
        ZoneId.of( "Africa/Tunis" )  // Get the current year as seen in the wall-clock time used by the people of a particular region (time zone). For any given moment, the date varies around the globe by zone.
    ) ,
    ChronoUnit.YEARS                 // Specify the granularity of the elapsed time using `ChronoUnit` enum.
)                                    // Return a `long`. 

57

Year

How to get current year in Android (min API version 15)

There’s a class for that!

Year shown in Java syntax (I don’t know Kotlin, yet)

Year.now()

Better to specify explicitly the desired/expected time zone rather than rely implicitly on your JVM’s current default time zone.

Year.now( 
    ZoneId.of( "Europe/Lisbon" ) 
) 

Pass this Year object around your code rather than mere integer numbers. This gives you type-safety, and makes your code more self-documenting. When you do need a number, call Year.getValue.

Year y = Year.now( ZoneId.systemDefault() ) ;
int yearNumber = y.getValue() ;

The modern approach uses the java.time classes rather than the troublesome old legacy date-time classes. Avoid Calendar, Date, SimpleDateFormat like the Plague.

For Java 6 & 7, see the ThreeTen-Backport project. For earlier Android, see the ThreeTenABP project.

Age

To calculate the age, parse your string input.

String input = "1961" ;
Year birthYear = Year.parse( input ) ;

Do the math. Specify a TemporalUnit (ChronoUnit) for the granularity of result.

Year now = Year.now( ZoneId.systemDefault() ) ;  // 2018 today.
long years = birthYear.until( now , ChronoUnit.YEARS ) ;

57


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.

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.

Where to obtain the java.time classes?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0
public fun calculateage(view: View){
        val dob: String = etbirth.text.toString()
        val sdf = SimpleDateFormat("yyyy")
        val currentyear1: String = sdf.format(Date()).toString()
     //   val currentyear = Calendar.get(Calendar.YEAR)
        val age = currentyear1.toInt() - dob.toInt()
        tvage.setText("$age")
    }

Thank you everyone for trying. thank you jyoti for your answer, this code is working perfectly.

s.nainwal
  • 21
  • 5
  • FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. Much of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Mar 24 '18 at 19:58