how to get current year in java code example
Example 1: java get current year
import java.util.Calendar;
int year = Calendar.getInstance().get(Calendar.YEAR);
Example 2: java get year from date
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(datetime);
SimpleDateFormat df = new SimpleDateFormat("yyyy");
year = df.format(date);
Example 3: java get year
import java.time.ZoneId;
import java.util.Date;
new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().getYear();
Example 4: get current day java
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);
switch (day) {
case Calendar.SUNDAY:
break;
case Calendar.MONDAY:
break;
case Calendar.TUESDAY:
break;
}