目录 一、代码示例二、截图示例 一、代码示例 package com.learning;import java.text.SimpleDateFORMat;import
package com.learning;import java.text.SimpleDateFORMat;import java.time.LocalDate;import java.time.Year;import java.util.Calendar;import java.util.Date;public class GetCurrentYear { public static void main(String[] args) { System.out.println(getCurrentYearByCalendar()); System.out.println(getCurrentYearByYear()); System.out.println(getCurrentYearBySimpleDateFormat()); System.out.println(getCurrentYearByLocalDate()); } public static String getCurrentYearByCalendar() { Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); return String.valueOf(year); } public static String getCurrentYearByYear(){ Year year = Year.now(); int yearValue = year.getValue(); return String.valueOf(yearValue); } public static String getCurrentYearBySimpleDateFormat(){ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy"); return simpleDateFormat.format(new Date()); } public static String getCurrentYearByLocalDate(){ LocalDate localDate = LocalDate.now(); int year = localDate.getYear(); return String.valueOf(year); }}
来源地址:https://blog.csdn.net/qq_32088869/article/details/132556276
--结束END--
本文标题: 【java】获取当前年份
本文链接: https://lsjlt.com/news/442425.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0