又抄一编
import java.util.*;public class Now {public static void main(String arg[]) {
/*
** on some JDK, the default TimeZone is wrong
** we must set the TimeZone manually!!!
** Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("EST"));
*/
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat(DATE_FORMAT);
/*
** on some JDK, the default TimeZone is wrong
** we must set the TimeZone manually!!!
** sdf.setTimeZone(TimeZone.getTimeZone("EST"));
*/
sdf.setTimeZone(TimeZone.getDefault());
System.out.println("Now : " + sdf.format(cal.getTime()));
}
}
Here some formatting possibilities available through the SimpleDateFormat class.
Thanks to T. Guirado for the tip. import java.util.*;
import java.text.*;public class ShowToday {
public static void main(String args[]) {
ShowToday st = new ShowToday();
st.demo();
}
public void demo() {
System.out.println(easyDateFormat("dd MMMMM yyyy"));
System.out.println(easyDateFormat("yyyyMMdd"));
System.out.println(easyDateFormat("dd.MM.yy"));
System.out.println(easyDateFormat("MM/dd/yy"));
System.out.println(easyDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z"));
System.out.println(easyDateFormat("EEE, MMM d, ''yy"));
System.out.println(easyDateFormat("h:mm a"));
System.out.println(easyDateFormat("H:mm:ss:SSS"));
System.out.println(easyDateFormat("K:mm a,z"));
System.out.println(easyDateFormat("yyyy.MMMMM.dd GGG hh:mm aaa"));
} public String easyDateFormat (String format) {
Date today = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(format);
String datenewformat = formatter.format(today);
return datenewformat;
}
}
import java.util.*;public class Now {public static void main(String arg[]) {
/*
** on some JDK, the default TimeZone is wrong
** we must set the TimeZone manually!!!
** Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("EST"));
*/
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat(DATE_FORMAT);
/*
** on some JDK, the default TimeZone is wrong
** we must set the TimeZone manually!!!
** sdf.setTimeZone(TimeZone.getTimeZone("EST"));
*/
sdf.setTimeZone(TimeZone.getDefault());
System.out.println("Now : " + sdf.format(cal.getTime()));
}
}
Here some formatting possibilities available through the SimpleDateFormat class.
Thanks to T. Guirado for the tip. import java.util.*;
import java.text.*;public class ShowToday {
public static void main(String args[]) {
ShowToday st = new ShowToday();
st.demo();
}
public void demo() {
System.out.println(easyDateFormat("dd MMMMM yyyy"));
System.out.println(easyDateFormat("yyyyMMdd"));
System.out.println(easyDateFormat("dd.MM.yy"));
System.out.println(easyDateFormat("MM/dd/yy"));
System.out.println(easyDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z"));
System.out.println(easyDateFormat("EEE, MMM d, ''yy"));
System.out.println(easyDateFormat("h:mm a"));
System.out.println(easyDateFormat("H:mm:ss:SSS"));
System.out.println(easyDateFormat("K:mm a,z"));
System.out.println(easyDateFormat("yyyy.MMMMM.dd GGG hh:mm aaa"));
} public String easyDateFormat (String format) {
Date today = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(format);
String datenewformat = formatter.format(today);
return datenewformat;
}
}
有一个Date(String)的构造子,
Date date=new Date("yyyy-mm-dd")但会给出警告。
建议用java.text.DateFormat类。
如
Date date=DateFormat.getDateTimeInstance().parse("7/10/99");