Open In App

Java SimpleDateFormat | Set 2

Last Updated : 17 Jan, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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

  • clone() : java.text.SimpleDateFormat.clone() creates a copy of the SimpleDateFormat.
Syntax :
public Object clone()
Parameters : 
-------
Return : 
copy of the SimpleDateFormat
Java
// 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());
 
    }
}

Output : 

Clone : java.text.SimpleDateFormat@a9427c06
  • 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.
Java
// 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());
 
    }
}

Output : 

Hash Code : -1455260666
  • 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
Java
// 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()));

    }
}

Output : 

Is equal ? : false
  • 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 : 
-------
Java
// 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");
        }        
    }
}

Output : 

Day1 : Sun
Day2 : Mon
Day3 : Tue
Day4 : Wed
Day5 : Thu
Day6 : Fri
Day7 : Sat
  • 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.
Java
// 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);
    }
}

Output : 

java.text.DateFormatSymbols@f86e64e0
Date : 6/24/17 12:49 PM
  • 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 : 
-------
Java
// Java Program illustrating 
// 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 calendar 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()); 
    }
}

Output : 

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



Next Article
Practice Tags :

Similar Reads