Java Tutorial/Development/Formatting Date Time — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:30, 31 мая 2010
Содержание
- 1 Display 12-hour time format
- 2 Display 24-hour time format.
- 3 Display complete time and date information: using %T rather than %t.
- 4 Display date using full names.
- 5 Display hour and minute, and include AM or PM indicator.
- 6 Display several time and date formats
- 7 Display short date format.
- 8 Formatting Time and Date: The Time and Date Format Suffixes
- 9 %tB %tb %tm: Display month by name and number
- 10 %tc: Display complete time and date information
- 11 The Time and Date Format Suffixes
- 12 %tl:%tM: Display just hour and minute
- 13 %tr: Formatting time and date
Display 12-hour time format
import java.util.Calendar;
import java.util.Formatter;
public class Main {
public static void main(String[] argv) throws Exception {
Formatter fmt = new Formatter();
Calendar cal = Calendar.getInstance();
fmt.format("Time using 12-hour clock: %tr\n", cal);
System.out.println(fmt);
}
}
//Time using 12-hour clock: 03:00:51 PM
Display 24-hour time format.
import java.util.Calendar;
import java.util.Formatter;
public class Main {
public static void main(String[] argv) throws Exception {
Formatter fmt = new Formatter();
Calendar cal = Calendar.getInstance();
fmt.format("Time using 24-hour clock: %tT\n", cal);
System.out.println(fmt);
}
}
//Time using 24-hour clock: 15:01:46
Display complete time and date information: using %T rather than %t.
import java.util.Calendar;
import java.util.Formatter;
public class Main {
public static void main(String[] argv) throws Exception {
Formatter fmt = new Formatter();
Calendar cal = Calendar.getInstance();
fmt.format("Time and date in lowercase: %tc\n", cal);
fmt.format("Time and date in uppercase: %Tc\n", cal);
System.out.println(fmt);
}
}
/*
Time and date in lowercase: Mon Mar 09 15:03:26 PDT 2009
Time and date in uppercase: MON MAR 09 15:03:26 PDT 2009
*/
Display date using full names.
import java.util.Calendar;
import java.util.Formatter;
public class Main {
public static void main(String[] argv) throws Exception {
Formatter fmt = new Formatter();
Calendar cal = Calendar.getInstance();
fmt.format("Long date format: ");
fmt.format("%tA %1$tB %1$td, %1$tY\n", cal);
System.out.println(fmt);
}
}
//Long date format: Monday March 09, 2009
Display hour and minute, and include AM or PM indicator.
import java.util.Calendar;
import java.util.Formatter;
public class Main {
public static void main(String[] argv) throws Exception {
Formatter fmt = new Formatter();
Calendar cal = Calendar.getInstance();
fmt.format("Hour and Minute: %tl:%1$tM %1$Tp\n", cal);
// Display the formatted times and dates.
System.out.println(fmt);
}
}
Display several time and date formats
import java.util.Calendar;
import java.util.Formatter;
public class Main {
public static void main(String[] argv) throws Exception {
Formatter fmt = new Formatter();
Calendar cal = Calendar.getInstance();
// Display 12-hour time format.
fmt.format("Time using 12-hour clock: %tr\n", cal);
System.out.println(fmt);
}
}
//Time using 12-hour clock: 03:00:51 PM
Display short date format.
import java.util.Calendar;
import java.util.Formatter;
public class Main {
public static void main(String[] argv) throws Exception {
Formatter fmt = new Formatter();
Calendar cal = Calendar.getInstance();
System.out.println(fmt.format("Short date format: %tD\n", cal));
}
}
//Short date format: 03/09/09
Formatting Time and Date: The Time and Date Format Suffixes
SuffixReplaced ByaAbbreviated weekday nameAFull weekday namebAbbreviated month nameBFull month namecStandard date and time string formatted as day month date hh::mm:ss tzone yearCFirst two digits of yeardDay of month as a decimal (01 to 31)Dmonth/day/yeareDay of month as a decimal (1 to 31)Fyear-month-dayhAbbreviated month nameHHour (00 to 23)IHour (01 to 12)jDay of year as a decimal (001 to 366)kHour (0 to 23)lHour (1 to 12)LMillisecond (000 to 999)mMonth as decimal (01 to 13)MMinute as decimal (00 to 59)NNanosecond (000000000 to 999999999)PLocale"s equivalent of AM or PM in uppercasepLocale"s equivalent of AM or PM in lowercaseQMilliseconds from 1/1/1970rhh:mm (12-hour format)Rhh:mm (24-hour format)SSeconds (00 to 60)sSeconds from 1/1/1970 UTCThh:mm:ss (24-hour format)yYear in decimal without century (00 to 99)YYear in decimal including century (0001 to 9999)zOffset from UTCZTime zone name
%tB %tb %tm: Display month by name and number
import java.util.Calendar;
import java.util.Formatter;
public class MainClass {
public static void main(String args[]) {
Formatter fmt = new Formatter();
Calendar cal = Calendar.getInstance();
fmt = new Formatter();
fmt.format("%tB %tb %tm", cal, cal, cal);
System.out.println(fmt);
}
}
December Dec 12
%tc: Display complete time and date information
import java.util.Calendar;
import java.util.Formatter;
public class MainClass {
public static void main(String args[]) {
Formatter fmt = new Formatter();
// .
fmt = new Formatter();
fmt.format("%tc", Calendar.getInstance());
System.out.println(fmt);
}
}
Fri Dec 01 08:58:44 PST 2006
The Time and Date Format Suffixes
Suffix Replaced By
a Abbreviated weekday name
A Full weekday name
b Abbreviated month name
B Full month name
c Standard date and time string formatted as day month date hh::mm:ss tzone year
C First two digits of year
d Day of month as a decimal (01-31)
D month/day/year
e Day of month as a decimal (1-31)
F year-month-day
h Abbreviated month name
H Hour (00 to 23)
I Hour (01 to 12)
j Day of year as a decimal (001 to 366)
k Hour (0 to 23)
l Hour (1 to 12)
L Millisecond (000 to 999)
m Month as decimal (01 to 13)
M Minute as decimal (00 to 59)
N Nanosecond (000000000 to 999999999)
p Locale"s equivalent of AM or PM in lowercase
Q Milliseconds from 1/1/1970
r hh:mm:ss (12-hour format)
R hh:mm (24-hour format)
S Seconds (00 to 60)
s Seconds from 1/1/1970 UTC
T hh:mm:ss (24-hour format)
y Year in decimal without century (00 to 99)
Y Year in decimal including century (0001 to 9999)
z Offset from UTC
Z Time zone name
%tl:%tM: Display just hour and minute
import java.util.Calendar;
import java.util.Formatter;
public class MainClass {
public static void main(String args[]) {
Formatter fmt = new Formatter();
Calendar cal = Calendar.getInstance();
// .
fmt = new Formatter();
fmt.format("%tl:%tM", cal, cal);
System.out.println(fmt);
}
}
8:59
%tr: Formatting time and date
import java.util.Calendar;
import java.util.Formatter;
public class MainClass {
public static void main(String args[]) {
Formatter fmt = new Formatter();
// Display standard 12-hour time format.
fmt.format("%tr", Calendar.getInstance());
System.out.println(fmt);
}
}
08:58:05 AM