Java SimpleDateFormat |设置 2
Java SimpleDateFormat |设置 1
text.SimpleDateFormat 类的更多方法:
- clone() : Java.text.SimpleDateFormat.clone()创建 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());
}
}
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());
}
}
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()));
}
}
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");
}
}
}
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);
}
}
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());
}
}
输出 :
Clone : java.text.SimpleDateFormat@a9427c06
- hashCode() : Java.text.SimpleDateFormat.hashCode()返回 SimpleDateFormat 对象的哈希码值。
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());
}
}
输出 :
Hash Code : -1455260666
- equals(Object obj) : Java.text.SimpleDateFormat.equals(Object obj)比较两个 SimpleDateFormat 对象
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()));
}
}
输出 :
Is equal ? : false
- setDateFormatSymbols(DateFormatSymbols newSymbols) : Java.text.SimpleDateFormat.setDateFormatSymbols(DateFormatSymbols newSymbols)设置所需日期格式的日期和时间格式符号。
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");
}
}
}
输出 :
Day1 : Sun
Day2 : Mon
Day3 : Tue
Day4 : Wed
Day5 : Thu
Day6 : Fri
Day7 : Sat
- getDateFormatSymbols() : Java.text.SimpleDateFormat.getDateFormatSymbols()返回日期和时间格式符号的副本。
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);
}
}
输出 :
java.text.DateFormatSymbols@f86e64e0
Date : 6/24/17 12:49 PM
- applyLocalizedPattern(String str) : Java.text.SimpleDateFormat.applyLocalizedPattern(String str)将给定的本地化字符串模式应用于日期格式。
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());
}
}
输出 :
Use of applyLocalizedPattern() : MM / dd / yy HH:mm Z