29

How can I display only the last two digits of the current year without using any substring algorithms or any third party libraries?

I have tried the below method and it gave a four-digit year. I want to know whether there are any date formatting options available to get the current year in two-digit format.

Calendar.getInstance().get(Calendar.YEAR);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
нαƒєєz
  • 1,239
  • 4
  • 17
  • 27

7 Answers7

76

You can simply use the modulo operator:

int lastTwoDigits = Calendar.getInstance().get(Calendar.YEAR) % 100;

Edit: Using a SimpleDateFormat, as @R.J proposed, is the better solution if you want the result to be a string. If you need an integer, use modulo.

Robin Krahl
  • 5,268
  • 19
  • 32
  • 10
    Seems like a clever solution at first but will result in a 1 digit number for years such as "2001" or 0 if it's like "2100" – Albert Tong Mar 08 '16 at 02:40
44

You can use a SimpleDateFormat to format a date as per your requirements.

DateFormat df = new SimpleDateFormat("yy"); // Just the year, with 2 digits
String formattedDate = df.format(Calendar.getInstance().getTime());
System.out.println(formattedDate);

Edit: Depending on the needs/requirements, either the approach suggested by me or the one suggested by Robin can be used. Ideally, when dealing with a lot of manipulations with the Date, it is better to use a DateFormat approach.

Rahul
  • 44,383
  • 11
  • 84
  • 103
15

This is a one-liner:

    System.out.println(Year.now().format(DateTimeFormatter.ofPattern("uu")));

I am using java.time.Year, one of a number of date and time classes introduced in Java 8 (and also backported to Java 6 and 7). This is just one little example out of very many where the new classes are more convenient and lead to clearer code than the old classes Calendar and SimpleDateFormat.

If you just wanted the two-digit number, not as a string, you may use:
Year.now().getValue() % 100.

The other answers were good answers in 2013, but the years have moved on. :-)

Edit: New Year doesn’t happen at the same time in all time zones. To make the dependency on time zone clear in the code I would usually write Year.now(myDesiredTimeZoneId), for example in the form Year.now(ZoneId.systemDefault()).

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
4

String.format() also has Date/Time Conversions.

To get the last two digits of the current year,

String.format("%ty", Year.now());

This works with a number of the java.time classes, including Year, YearMonth, LocalDate, LocalDateTime, and ZonedDateTime.

jaco0646
  • 15,303
  • 7
  • 59
  • 83
1

In Kotlin

Call this function and pass time in parameter

Example :
Input Parameter : "2021-08-09T16:01:38.905Z"
Output : 09 Aug 21

private val inputFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
private val outputFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")

        fun calculateDateMonthYear(time: String): String {
            val inputTime = inputFormat.parse(time)
            val convertDateMonthYear = outputFormat.format(inputTime!!)
            val timeInMilliseconds = outputFormat.parse(convertDateMonthYear)!!
            val mTime: Calendar = Calendar.getInstance()
            mTime.timeInMillis = timeInMilliseconds.time
            val date_cal = SimpleDateFormat("dd")
            val month_date = SimpleDateFormat("MMM")
            val year_cal = SimpleDateFormat("yy")
            val date = date_cal.format(mTime.time)
            val month_name = month_date.format(mTime.time)
            val year = year_cal.format(mTime.time)
            return "$date $month_name $year"
        }
Tippu Fisal Sheriff
  • 2,177
  • 11
  • 19
0

The solution of @Robin Krahl is a bit hacky but I like it. We could avoid the cases commented by @Albert Tong if we add a leading zero. In order that we have the last two digits of the year, I suppose that the result should be a String. In case we could not use the solution of @jaco0646. We could do this:

 String yearStr = addLeadingZero(Calendar.getInstance().get(Calendar.YEAR) % 100)

 private String addLeadingZero(int num) {
   String result = String.valueOf(num);
    if(num < 10){
        result = "0" + result;
    }

    return result;
}
MeLean
  • 3,092
  • 6
  • 29
  • 43
0

I know the question asked for how to do this in Java but Google sent me here when asking about Groovy, so I'll add my answer for that here.

I came up with this based on the Java answer provided by Rahul:

String lastTwoYearDigits = new Date().format("yy")
Daniel Black
  • 550
  • 6
  • 13