Java SimpleDateFormat | Set 2

Java SimpleDateFormat | Set 1
More methods of text.SimpleDateFormat class :

  1. clone() : java.text.SimpleDateFormat.clone() creates a copy of the SimpleDateFormat.
    Syntax :
    public Object clone()
    Parameters : 
    -------
    Return : 
    copy of the SimpleDateFormat
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program illustrating 
    // use of SimpleDateFormat.clone() 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();
                      
            // Use of clone() : 
           System.out.println("Clone : " + geek.clone());
       
        }
    }

    chevron_right

    
    

    Output :

    Clone : java.text.SimpleDateFormat@a9427c06
    
  2. hashCode() : java.text.SimpleDateFormat.hashCode() returns hash code value for the SimpleDateFormat object.
    Syntax :
    public int hashCode()
    Parameters : 
    --------
    Return : 
    hash code value for the SimpleDateFormat object.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program illustrating 
    // use of SimpleDateFormat.hashCode() 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();
                      
            // Use of hashCode() : 
           System.out.println("Hash Code : " + geek.hashCode());
       
        }
    }

    chevron_right

    
    

    Output :



    Hash Code : -1455260666
    
  3. equals(Object obj) : java.text.SimpleDateFormat.equals(Object obj) compares two SimpleDateFormat objects
    Syntax :
    public boolean equals(Object obj)
    Parameters : 
    obj : object to compare with
    Return : 
    true, if equal ; else false
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program illustrating 
    // use of SimpleDateFormat.equals() method
      
    import java.text.*;
    import java.util.Calendar;
       
    public class NewClass
    {
        public static void main(String[] args) 
                    throws InterruptedException, ParseException
        {
              
        // Setting formatter  
        SimpleDateFormat geek = new SimpleDateFormat();
      
        geek.applyPattern("MMM");
          
        // Use of equals() :  
        System.out.println("Is equal ? : "
                          geek.equals(new SimpleDateFormat()));
      
        }
    }

    chevron_right

    
    

    Output :

    Is equal ? : false
    
  4. setDateFormatSymbols(DateFormatSymbols newSymbols) : java.text.SimpleDateFormat.setDateFormatSymbols(DateFormatSymbols newSymbols) sets date and time format symbols of the required date format.
    Syntax :
    public void setDateFormatSymbols(DateFormatSymbols newSymbols)
    Parameters : 
    newSymbols : new format symbols for data and time
    Return : 
    -------
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program illustrating 
    // use of DateFormatSymbols() method
      
    import java.text.*;
    import java.util.*;
      
    public class NewClass
    {
        public static void main(String[] args) throws InterruptedException, ParseException
        {
      
            DateFormatSymbols format_symb = 
                         new DateFormatSymbols(new Locale("en", "US"));
            String[] days = format_symb.getShortWeekdays();
      
            int j = 1;
            for (int i = 1; i < days.length; i++)
            {
      
                System.out.print("Day" + j++ + " : " + days[i] + "\n");
            }        
        }
    }

    chevron_right

    
    

    Output :

    Day1 : Sun
    Day2 : Mon
    Day3 : Tue
    Day4 : Wed
    Day5 : Thu
    Day6 : Fri
    Day7 : Sat
    
  5. getDateFormatSymbols() : java.text.SimpleDateFormat.getDateFormatSymbols()returns the copy of the date and time format symbols.
    Syntax :
    public DateFormatSymbols getDateFormatSymbols()
    Parameters : 
    -------
    Return : 
    date and time format symbols.
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program illustrating 
    // use of getDateFormatSymbols() method
      
    import java.text.*;
    import java.util.*;
      
    public class NewClass
    {
        public static void main(String[] args) 
                       throws InterruptedException, ParseException
        {
            SimpleDateFormat geeks = new SimpleDateFormat();
      
            String g_date = geeks.format(new Date());
              
            // Use of getDateFormatSymbols () : 
            System.out.println("" + geeks.getDateFormatSymbols());
            System.out.println("Date : " + g_date);
        }
    }

    chevron_right

    
    

    Output :

    java.text.DateFormatSymbols@f86e64e0
    Date : 6/24/17 12:49 PM
    
  6. applyLocalizedPattern(String str) : java.text.SimpleDateFormat.applyLocalizedPattern(String str) applies the given localized string pattern to date format.
    Syntax :
    public void applyLocalizedPattern(String str)
    Parameters : 
    str : string pattern set to new date and time format
    Return : 
    -------
    
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program illustarting 
    // use of applyLocalizedPattern() method
       
    import java.text.*;
    import java.util.Calendar;
       
    public class NewClass
    {
        public static void main(String[] args) 
                                    throws InterruptedException
        {        
            SimpleDateFormat geek = new SimpleDateFormat();
            // Initializing calender Object
            Calendar c = Calendar.getInstance();
       
            // Using 'arg' pattern
            String arg = "MM / dd / yy  HH:mm Z";
       
            // Use of applyLocalizedPattern()
            geek.applyLocalizedPattern(arg);
      
         System.out.println("Use of applyLocalizedPattern() 
                                 : "+geek.toLocalizedPattern()); 
        }
    }

    chevron_right

    
    

    Output :

    Use of applyLocalizedPattern() : MM / dd / yy  HH:mm Z
    

This article is contributed by Mohit Gupta_OMG 😀. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.