使用SimpleDateFormat在Java中显示今天的日期
要使用Java中的SimpleDateFormat类,请导入以下包。
import java.text.SimpleDateFormat;
要获取日期,请使用format()如下所示的方法。首先,设置日期格式-
Calendar cal = Calendar.getInstance();
SimpleDateFormat simpleformat = new SimpleDateFormat("dd/MM/yyyy");现在,我们将获得日期-
simpleformat.format(cal.getTime())
以下是完整的示例-
示例
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Demo {
public static void main(String[] args) throws Exception {
Calendar cal = Calendar.getInstance();
SimpleDateFormat simpleformat = new SimpleDateFormat("dd/MM/yyyy");
System.out.println("Today's date = "+simpleformat.format(cal.getTime()));
}
}输出结果
Today's date = 26/11/2018
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短