在 Java 中以 yy 格式设置年份
以 yy 格式设置年份是指将年份显示为 01、02、03、04 等。例如,将 2018 年显示为 18。
请按以下方法使用 yy 格式。
SimpleDateFormat("yy");让我们来看一个示例 -
// year in yy format
SimpleDateFormat simpleformat = new SimpleDateFormat("yy");
String strYear = simpleformat.format(new Date());
System.out.println("Current Year = "+strYear);上面,我们使用了 SimpleDateFormat 类,因此,我们导入了以下程序包 -
import java.text.SimpleDateFormat;
这是一个示例 -
示例
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
public class Demo {
public static void main(String[] args) throws Exception {
// displaying current date and time
Calendar cal = Calendar.getInstance();
SimpleDateFormat simpleformat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss");
System.out.println("Date and time = "+simpleformat.format(cal.getTime()));
// displaying date
simpleformat = new SimpleDateFormat("dd/MMMM/yyyy");
String str = simpleformat.format(new Date());
System.out.println("Current Date = "+str);
// year in yy format
simpleformat = new SimpleDateFormat("yy");
String strYear = simpleformat.format(new Date());
System.out.println("Current Year = "+strYear);
}
}输出
Date and time = Mon, 26 Nov 2018 11:05:15 Current Date = 26/November/2018 Current Year = 18
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP