Bài 257: SimpleDateFormat trong java

Ngày đăng: 1/6/2023 3:24:44 PM

Có hai lớp để định dạng ngày trong java: DateFormat và SimpleDateFormat.

Lớp java.text.SimpleDateFormat trong java cung cấp các phương thức để định dạng và phân tích ngày tháng và thời gian trong java. SimpleDateFormat kế thừa lớp java.text.DateFormat.

Lưu ý: rằng định dạng (format) có nghĩa là chuyển đổi date thành string và phân tích (parse) có nghĩa là chuyển đổi string thành date.

SimpleDateFormat trong java

Phương thức format() và parse() của lớp SimpleDateFormat:

  • Phương thức SimpleDateFormat.format(java.util.Date date): được sử dụng để chuyển đổi date thành string trong java.
  • Phương thức SimpleDateFormat.parse(String string): được dụng để phân tích string thành date trong java.

Ví dụ SimpleDateFormat trong java - chuyển đổi date thành string

Dưới đây là ví dụ định dạng date trong java theo pattern bằng việc sử dụng lớp java.text.SimpleDateFormat:

1

2

3

4

5

6

7

8

9

10

11

12

13    

package vn.viettuts.date;

 

import java.text.SimpleDateFormat;

import java.util.Date;

 

public class SimpleDateFormatExample1 {

    public static void main(String[] args) {

        Date date = new Date();

        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

        String strDate = formatter.format(date);

        System.out.println(strDate);

    }

}

Kết quả:

 11/09/2017
 

Note: M (chữ hoa) biển diễn tháng, còn m (m thường) biểu diễn phút trong java.

Dưới đây là ví dụ đầy đủ định dạng date trong java theo pattern bằng việc sử dụng lớp java.text.SimpleDateFormat:

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

26

27

28

29

30    

package vn.viettuts.date;

 

import java.text.SimpleDateFormat;

import java.util.Date;

 

public class SimpleDateFormatExample2 {

    public static void main(String[] args) {

        Date date = new Date();

        SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");

        String strDate = formatter.format(date);

        System.out.println("Date Format with MM/dd/yyyy: " + strDate);

 

        formatter = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");

        strDate = formatter.format(date);

        System.out.println("Date Format with dd-M-yyyy hh:mm:ss: " + strDate);

 

        formatter = new SimpleDateFormat("dd MMMM yyyy");

        strDate = formatter.format(date);

        System.out.println("Date Format with dd MMMM yyyy: " + strDate);

 

        formatter = new SimpleDateFormat("dd MMMM yyyy zzzz");

        strDate = formatter.format(date);

        System.out.println("Date Format with dd MMMM yyyy zzzz: " + strDate);

 

        formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");

        strDate = formatter.format(date);

        System.out.println("Date Format with E, dd MMM yyyy HH:mm:ss z: "

                + strDate);

    }

}

Kết quả:

 Date Format with MM/dd/yyyy: 09/11/2017
 Date Format with dd-M-yyyy hh:mm:ss: 11-9-2017 11:06:23
 Date Format with dd MMMM yyyy: 11 September 2017
 Date Format with dd MMMM yyyy zzzz: 11 September 2017 Indochina Time
 Date Format with E, dd MMM yyyy HH:mm:ss z: Mon, 11 Sep 2017 11:06:23 ICT
 

Ví dụ SimpleDateFormat trong java - chuyển đổi string thành date

Dưới đây là ví dụ parse string thành date trong java bằng việc sử dụng lớp java.text.SimpleDateFormat:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17     

package vn.viettuts.date;

 

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

 

public class SimpleDateFormatExample3 {

    public static void main(String[] args) {

        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

        try {

            Date date = formatter.parse("31/09/2017");

            System.out.println("Date: " + date);

        } catch (ParseException e) {

            e.printStackTrace();

        }

    }

}

Kết quả:

 Date: Sun Oct 01 00:00:00 ICT 2017

Nguồn tin viettuts.vn