用Java显示当前年份的最后两位数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20070258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying the last two digits of the current year in Java
提问by нα???z
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);
采纳答案by SudoRahul
You can use a SimpleDateFormat
to format a date as per your requirements.
您可以SimpleDateFormat
根据您的要求使用 a来格式化日期。
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 Robincan be used. Ideally, when dealing with a lot of manipulations with the Date, it is better to use a DateFormat
approach.
编辑:根据需要/要求,可以使用我建议的方法或Robin建议的方法。理想情况下,当处理大量的 Date 操作时,最好使用一种DateFormat
方法。
回答by Robin Krahl
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.
编辑:SimpleDateFormat
如果您希望结果为字符串,则使用@RJ 建议的a是更好的解决方案。如果您需要一个整数,请使用模数。
回答by Ole V.V.
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
.
我正在使用java.time.Year
Java 8 中引入的许多日期和时间类之一(并且还向后移植到 Java 6 和 7)。这只是一个小例子了很多地方的新类更方便,导致比老班更清晰的代码Calendar
和SimpleDateFormat
。
If you just wanted the two-digit number, not as a string, you may use:Year.now().getValue() % 100
.
如果您只想要两位数,而不是字符串,则可以使用:Year.now().getValue() % 100
。
The other answers were good answers in 2013, but the years have moved on. :-)
其他答案在 2013 年都是不错的答案,但这些年已经过去了。:-)
回答by jaco0646
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
.
这一方法在数量的java.time
类别,包括Year
,YearMonth
,LocalDate
,LocalDateTime
,和ZonedDateTime
。