java中怎么把日期格式化

内容摘要
java把日期格式化:import java.text.SimpleDateFormat;
import java.util.Date;

public class test{

public static void main(String args[]) {
Date newTime
文章正文

java把日期格式化:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class test{
 
    public static void main(String args[]) {
        Date newTime = new Date();
        //设置时间格式
        SimpleDateFormat sdf1 = new SimpleDateFormat("y-M-d h:m:s a E");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yy-MM-dd hh:mm:ss a E");
        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MMM-ddd hhh:mmm:sss a E");
        SimpleDateFormat sdf4 = new SimpleDateFormat("yyyyy-MMMM-dddd hhhh:mmmm:ssss a E");
         
        //获取的时间,是本机的时间
        String formatDate1 = sdf1.format(newTime);
        String formatDate2 = sdf2.format(newTime);
        String formatDate3 = sdf3.format(newTime);
        String formatDate4 = sdf4.format(newTime);
 
        System.out.println(formatDate1); 
        System.out.println(formatDate2);
        System.out.println(formatDate3);
        System.out.println(formatDate4);
    }
}

输出结果如下:

1.jpg

java中使用SimpleDateFormat类的构造函数SimpleDateFormat(String str)构造格式化日期的格式,

通过format(Date date)方法将指定的日期对象格式化为指定格式的字符串.

更多java知识请关注java基础教程。

代码注释
[!--zhushi--]

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!