java.time
The java.time classes built into Java 8, Java 9, and later provide these helpful classes:
Year
class to represent a year
DateTimeFormatter
class to create strings representing a date-time value in a specific format.
FormatStyle
enum to flag longer or shorter formats.
The Year
class might seem simplistic and gratuitous at first, but using it makes your code more self-documenting than passing around mere integer numbers.
You can use those to help in representing your academic year. Indeed, you might want to create your own class such as seen below. Look to the toString
format
methods specifically to answer the Question.
Using the class.
YearAcademic.now( ZoneId.of( "Africa/Tunis" ) )
.format( FormatStyle.SHORT )
Implementation of format
.
String s;
if ( ( formatStyle == FormatStyle.FULL ) || ( formatStyle == FormatStyle.LONG ) ) // Generate a string in format of YYYY/YYYY such as 2017/2018.
{
s = this.start + "/" + this.stop;
} else if ( ( formatStyle == FormatStyle.MEDIUM ) || ( formatStyle == FormatStyle.SHORT ) ) // Generate a string in format of YYYY/YY such as 2017/18.
{
s = this.start + "/" + this.stop.format( this.doubleDigitYearFormatter );
} else
{
// Handle error, what should be unreachable code.
s = "ERROR";
System.out.println( "ERROR - Reached IF-ELSE for format style argument: " + formatStyle );
}
Full class.
package com.example.javatimestuff;
import java.time.Year;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class YearAcademic
{
Year start, stop;
private DateTimeFormatter doubleDigitYearFormatter = DateTimeFormatter.ofPattern( "uu" );
// Constructor
public YearAcademic ( Year startArg )
{
this.start = startArg;
this.stop = this.start.plusYears( 1L );
}
// Constructor
public YearAcademic ( Year startArg , Year stopArg )
{
this.start = startArg;
this.stop = stopArg;
}
// Constructor
public YearAcademic ( int startArg , int stopArg )
{
this.start = Year.of( startArg );
this.stop = Year.of( stopArg );
}
// Constructor
public YearAcademic ( int startArg )
{
this.start = Year.of( startArg );
this.stop = this.start.plusYears( 1L );
}
// Constructor
public static YearAcademic now ( ZoneId zoneId )
{
YearAcademic ya = new YearAcademic( Year.now( zoneId ) );
return ya;
}
@Override
public String toString ( )
{
String s = this.format( FormatStyle.FULL );
return s;
}
public String format ( FormatStyle formatStyle )
{
String s;
if ( ( formatStyle == FormatStyle.FULL ) || ( formatStyle == FormatStyle.LONG ) ) // Generate a string in format of YYYY/YYYY such as 2017/2018.
{
s = this.start + "/" + this.stop;
} else if ( ( formatStyle == FormatStyle.MEDIUM ) || ( formatStyle == FormatStyle.SHORT ) ) // Generate a string in format of YYYY/YY such as 2017/18.
{
s = this.start + "/" + this.stop.format( this.doubleDigitYearFormatter );
} else
{
// Handle error, what should be unreachable code.
s = "ERROR";
System.out.println( "ERROR - Reached IF-ELSE for format style argument: " + formatStyle );
}
return s;
}
public static void main ( String[] args )
{
YearAcademic ya = YearAcademic.now( ZoneId.systemDefault( ) );
System.out.println( "Academic year now: " + ya.format( FormatStyle.SHORT ) );
}
}
Academic year now: 2017/18
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.
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.