📌  相关文章
📜  Java SimpleDateFormat |设置 1

📅  最后修改于: 2022-05-13 01:55:35.592000             🧑  作者: Mango

Java SimpleDateFormat |设置 1

SimpleDateFormat 类有助于格式化和解析数据。我们可以将日期从一种格式更改为另一种格式。它允许用户将字符串日期格式解释为 Date 对象。我们可以根据需要相应地修改日期。

宣言 :

public class SimpleDateFormat
extends DateFormat

构造函数:

  • SimpleDateFormat(String pattern_arg) :使用给定的模式构造简单的日期格式 - pattern_arg,默认格式语言环境的默认日期格式符号。
  • SimpleDateFormat(String pattern_arg, Locale locale_arg) :使用给定模式 - pattern_arg 构造简单日期格式,给定 FORMAT Locale - locale_arg 的默认日期格式符号。
  • SimpleDateFormat(String pattern_arg, DateFormatSymbols formatSymbols) :使用给定的模式 - pattern_arg 和日期格式符号构造 SimpleDateFormat。

说明 SimpleDateFormat 类的Java程序

Java
// Java Program illustrating SimpleDateFormat class
import java.text.*;
import java.util.*;
 
public class NewClass
{
    public static void main(String[] args)
    {
        SimpleDateFormat geek = new SimpleDateFormat("dd / MM / yy");
 
        // Creating instance of the System date
        Calendar c = Calendar.getInstance();
        System.out.println("Present Date : " + c.getTime());
 
        // Formatting Date according "dd / MM / yy"
        String formattedDate = geek.format(c.getTime());
        System.out.println("Date formatted : "+formattedDate);
 
    }
}


Java
// Java Program illustrating
// use of set2DigitYearStart() method
 
import java.text.*;
import java.util.Calendar;
 
public class NewClass
{
    public static void main(String[] args) throws InterruptedException
    {
        SimpleDateFormat geek = new SimpleDateFormat("MM / dd / yy");
        try
        {
            Calendar c = Calendar.getInstance();
            c.setTime(geek.parse("10 / 27 / 16"));
            System.out.println("Initial Time : "+c.getTime());
 
            // Setting 1916 instead of 2016
            // Use of set2DigitYearStart() method
            geek.set2DigitYearStart(geek.parse("01 / 01 / 1900"));
 
            c.setTime(geek.parse("06 / 12 / 16"));
            System.out.println("New Time : "+c.getTime());
 
        }
        catch (ParseException except)
        {
            except.printStackTrace();
        }
    }
}


Java
// Java Program illustrating
// use of get2DigitYearStart() method
 
import java.text.*;
import java.util.Calendar;
 
public class NewClass
{
    public static void main(String[] args) throws InterruptedException
    {
        SimpleDateFormat geek = new SimpleDateFormat("MM / dd / yy");
        try
        {
            Calendar c = Calendar.getInstance();
            c.setTime(geek.parse("10 / 27 / 16"));       
            System.out.println("Initial Time : "+c.getTime());
             
            // Setting 1916 instead of 2016
            // Use of set2DigitYearStart() method
            geek.set2DigitYearStart(geek.parse("01 / 01 / 1900"));
            // Start Year is 1990.
 
            c.setTime(geek.parse("06 / 12 / 16"));
            System.out.println("New Time : "+c.getTime());
             
            // Use of get2DigitYearStart() method to check start year
            c.setTime(geek.get2DigitYearStart());
            System.out.println("START Year : "+c.get(Calendar.YEAR));
             
        }
        catch (ParseException except)
        {
            except.printStackTrace();
        }   
    }
}


Java
// Java Program illustrating use of toPattern() method
 
import java.text.*;
import java.util.Calendar;
 
public class NewClass
{
    public static void main(String[] args) throws InterruptedException
    {
        SimpleDateFormat geek = new SimpleDateFormat();
 
        // Initializing Calendar object
        Calendar c = Calendar.getInstance();
 
        // getting Current Date
        String dateToday = geek.format(c.getTime());
 
        System.out.println("Current Date : "+dateToday);
 
        // Use of toPattern() method
        // Printing Date Pattern
        System.out.println("Date Pattern : "+geek.toPattern());
 
    }
}


Java
// Java Program illustrating
// use of parse() method
 
import java.text.*;
import java.util.Calendar;
 
public class NewClass
{
    public static void main(String[] args) throws InterruptedException
    {
        SimpleDateFormat geek = new SimpleDateFormat("MM / dd / yy");
        try
        {
            Calendar c = Calendar.getInstance();
 
            // Use of .parse() method to parse Date From String 's'
            String s = "10 / 27 / 16" ;
             
            c.setTime(geek.parse(s));
            System.out.println("Time parsed from String : "+c.getTime());
        }
        catch (ParseException except)
        {
            except.printStackTrace();
        }
    }
}


Java
// Java Program illustrating
// use of applyPattern() method
 
import java.text.*;
import java.util.Calendar;
public class NewClass
{
    public static void main(String[] args) throws InterruptedException
    {
        SimpleDateFormat geek = new SimpleDateFormat();
 
        // Initializing calendar Object
        Calendar c = Calendar.getInstance();
 
        // Using 'arg' pattern
        String arg = "dd / MM / yyyy HH:mm Z";
 
        // Use of applyPattern() method to set date to 'arg' format
        geek.applyPattern(arg);
 
        // current date and time
        String currentdate = geek.format(c.getTime());
        System.out.println("Current Date : "+currentdate);
 
        // Print the pattern being used
        System.out.println("Pattern applied : "+geek.toPattern());
    }
}


Java
// Java Program illustrating
// use of format() method
 
import java.text.*;
import java.util.Calendar;
 
public class NewClass
{
    public static void main(String[] args) throws InterruptedException
    {
        SimpleDateFormat geek = new SimpleDateFormat();
 
        // Initializing calendar Object
        Calendar c = Calendar.getInstance();
         
        System.out.println("Actual Date : "+c.getTime());
 
        // Use of format() method to format Date to String
        String currentdate = geek.format(c.getTime());
        System.out.println("Formatted Date to String : "+currentdate);
 
    }
}


Java
// Java Program illustrating
// use of toLocalizedPattern() method
 
import java.text.*;
import java.util.Calendar;
 
public class NewClass
{
    public static void main(String[] args) throws InterruptedException
    {
        // Date Formatter
        SimpleDateFormat geek = new SimpleDateFormat();
 
        // Initializing calendar Object
        Calendar c = Calendar.getInstance();
         
        System.out.println("Date : " + geek.format(c.getTime()));
 
        // Use of format() method to format Date to String
        System.out.println("Pattern in DateFormater 'geek' :"
                             + geek.toLocalizedPattern());
 
    }
}


输出:

Present Date : Wed Jun 21 18:21:13 IST 2017
Date formatted : 21 / 06 / 17

方法 :

1. set2DigitYearStart() : Java.text.SimpleDateFormat.set2DigitYearStart(Date starting_Date)解析日期并将日期设置在starting_Date到starting_Date + 100年的范围内。

句法 :

public void set2DigitYearStart(Date starting_Date)
Parameters :
starting_Date : Date is set in the range - starting_Date to starting_Date + 100 years
Return  :
Returns void

Java

// Java Program illustrating
// use of set2DigitYearStart() method
 
import java.text.*;
import java.util.Calendar;
 
public class NewClass
{
    public static void main(String[] args) throws InterruptedException
    {
        SimpleDateFormat geek = new SimpleDateFormat("MM / dd / yy");
        try
        {
            Calendar c = Calendar.getInstance();
            c.setTime(geek.parse("10 / 27 / 16"));
            System.out.println("Initial Time : "+c.getTime());
 
            // Setting 1916 instead of 2016
            // Use of set2DigitYearStart() method
            geek.set2DigitYearStart(geek.parse("01 / 01 / 1900"));
 
            c.setTime(geek.parse("06 / 12 / 16"));
            System.out.println("New Time : "+c.getTime());
 
        }
        catch (ParseException except)
        {
            except.printStackTrace();
        }
    }
}

输出 :

Initial Time : Thu Oct 27 00:00:00 IST 2016
New Time : Mon Jun 12 00:00:00 IST 1916

2. get2DigitYearStart() : Java.text.SimpleDateFormat.get2DigitYearStart()返回解析期间设置的 100 年周期的开始。

句法 :

public void get2DigitYearStart()
Parameters :
-----
Return  :
Returns start of 100 year period that was set during parsing.

执行 :

Java

// Java Program illustrating
// use of get2DigitYearStart() method
 
import java.text.*;
import java.util.Calendar;
 
public class NewClass
{
    public static void main(String[] args) throws InterruptedException
    {
        SimpleDateFormat geek = new SimpleDateFormat("MM / dd / yy");
        try
        {
            Calendar c = Calendar.getInstance();
            c.setTime(geek.parse("10 / 27 / 16"));       
            System.out.println("Initial Time : "+c.getTime());
             
            // Setting 1916 instead of 2016
            // Use of set2DigitYearStart() method
            geek.set2DigitYearStart(geek.parse("01 / 01 / 1900"));
            // Start Year is 1990.
 
            c.setTime(geek.parse("06 / 12 / 16"));
            System.out.println("New Time : "+c.getTime());
             
            // Use of get2DigitYearStart() method to check start year
            c.setTime(geek.get2DigitYearStart());
            System.out.println("START Year : "+c.get(Calendar.YEAR));
             
        }
        catch (ParseException except)
        {
            except.printStackTrace();
        }   
    }
}

输出 :

Initial Time : Thu Oct 27 00:00:00 IST 2016
New Time : Mon Jun 12 00:00:00 IST 1916
START Year : 1900

3. toPattern() : Java.text.SimpleDateFormat.toPattern()返回日期格式的模式。

句法 :

public String toPattern()
Parameters :
-----
Return  :
Returns pattern of the Date format.

Java

// Java Program illustrating use of toPattern() method
 
import java.text.*;
import java.util.Calendar;
 
public class NewClass
{
    public static void main(String[] args) throws InterruptedException
    {
        SimpleDateFormat geek = new SimpleDateFormat();
 
        // Initializing Calendar object
        Calendar c = Calendar.getInstance();
 
        // getting Current Date
        String dateToday = geek.format(c.getTime());
 
        System.out.println("Current Date : "+dateToday);
 
        // Use of toPattern() method
        // Printing Date Pattern
        System.out.println("Date Pattern : "+geek.toPattern());
 
    }
}

输出 :

Current Date : 6/21/17 6:24 PM
Date Pattern : M/d/yy h:mm a

4. parse() : Java.text.SimpleDateFormat.parse()从字符串中解析文本以形成日期。它由类 SimpleDateFormat 中的 parse 指定。

句法 :

public Date parse()
Parameters :
-----
Return  :
Returns Date parsed from a string.

Java

// Java Program illustrating
// use of parse() method
 
import java.text.*;
import java.util.Calendar;
 
public class NewClass
{
    public static void main(String[] args) throws InterruptedException
    {
        SimpleDateFormat geek = new SimpleDateFormat("MM / dd / yy");
        try
        {
            Calendar c = Calendar.getInstance();
 
            // Use of .parse() method to parse Date From String 's'
            String s = "10 / 27 / 16" ;
             
            c.setTime(geek.parse(s));
            System.out.println("Time parsed from String : "+c.getTime());
        }
        catch (ParseException except)
        {
            except.printStackTrace();
        }
    }
}

输出 :

Time parsed from String : Thu Oct 27 00:00:00 IST 2016

5. applyPattern() : Java.text.SimpleDateFormat.applyPattern(String arg)用于将定义的模式设置为日期格式。

句法 :

public void applyPattern(String arg)
Parameters :
arg : defined pattern to be set to the Date Format.
Return  :
Void

执行 :

Java

// Java Program illustrating
// use of applyPattern() method
 
import java.text.*;
import java.util.Calendar;
public class NewClass
{
    public static void main(String[] args) throws InterruptedException
    {
        SimpleDateFormat geek = new SimpleDateFormat();
 
        // Initializing calendar Object
        Calendar c = Calendar.getInstance();
 
        // Using 'arg' pattern
        String arg = "dd / MM / yyyy HH:mm Z";
 
        // Use of applyPattern() method to set date to 'arg' format
        geek.applyPattern(arg);
 
        // current date and time
        String currentdate = geek.format(c.getTime());
        System.out.println("Current Date : "+currentdate);
 
        // Print the pattern being used
        System.out.println("Pattern applied : "+geek.toPattern());
    }
}

输出 :

Current Date : 21 / 06 / 2017 18:25 +0530
Pattern applied : dd / MM / yyyy HH:mm Z

6. format() : Java.text.SimpleDateFormat.format(Date arg)将 Date 或 Time 更改为字符串。

句法 :

public final String format(Date arg)
Parameters :
arg : Date to be formatted to String
Return  :
Formatted String of Date

Java

// Java Program illustrating
// use of format() method
 
import java.text.*;
import java.util.Calendar;
 
public class NewClass
{
    public static void main(String[] args) throws InterruptedException
    {
        SimpleDateFormat geek = new SimpleDateFormat();
 
        // Initializing calendar Object
        Calendar c = Calendar.getInstance();
         
        System.out.println("Actual Date : "+c.getTime());
 
        // Use of format() method to format Date to String
        String currentdate = geek.format(c.getTime());
        System.out.println("Formatted Date to String : "+currentdate);
 
    }
}

输出 :

Actual Date : Wed Jun 21 18:25:50 IST 2017
Formatted Date to String : 6/21/17 6:25 PM

7. toLocalizedPattern() : Java.text.SimpleDateFormat.toLocalizedPattern()返回日期格式器的日期模式字符串。

句法 :

public String toLocalizedPattern()
Parameters :
------
Return  :
Date pattern String used in the formatter

Java

// Java Program illustrating
// use of toLocalizedPattern() method
 
import java.text.*;
import java.util.Calendar;
 
public class NewClass
{
    public static void main(String[] args) throws InterruptedException
    {
        // Date Formatter
        SimpleDateFormat geek = new SimpleDateFormat();
 
        // Initializing calendar Object
        Calendar c = Calendar.getInstance();
         
        System.out.println("Date : " + geek.format(c.getTime()));
 
        // Use of format() method to format Date to String
        System.out.println("Pattern in DateFormater 'geek' :"
                             + geek.toLocalizedPattern());
 
    }
}

输出 :

Date : 6/21/17 6:27 PM
Pattern in DateFormater 'geek' : M/d/yy h:mm a

下一篇: Java.text.SimpleDateFormat 类 |设置 2