import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class Datetime {
public String getDatetime_String1() {
String datetime = new Date().toString();
return datetime;
}
public String getDatetime_String2() {
String datetime = new Date().toGMTString();
return datetime;
}
public String getDatetime_System() {
DateFormat dt = DateFormat.getInstance();
String datetime = dt.format(new Date()).toString();
return datetime;
}
public String getDatetime_China() {
DateFormat datetime1 = DateFormat.getDateInstance(DateFormat.LONG,
Locale.CHINA);
DateFormat datetime2 = DateFormat.getTimeInstance(DateFormat.LONG,
Locale.CHINA);
String datetime = datetime1.format(new Date()) + " "
+ datetime2.format(new Date());
return datetime;
}
public String getDatetime_Standard() {
Calendar now = Calendar.getInstance();
String datetime = now.get(Calendar.YEAR) + "-"
+ (now.get(Calendar.MONTH) + 1) + "-"
+ now.get(Calendar.DAY_OF_MONTH) + " " + now.get(Calendar.HOUR)
+ ":" + now.get(Calendar.MINUTE) + ":"
+ now.get(Calendar.SECOND) + now.get(Calendar.MILLISECOND);
return datetime;
}
public String getDatetime() {
String datetime = new Date().toLocaleString();
return datetime;
}
public String getDatetime_SimpleDateFormat() {
try {
SimpleDateFormat f = new java.text.SimpleDateFormat("yyyy-MM-dd");
String sDate = f.format(new java.util.Date());
java.util.Date dt = f.parse(sDate);
java.sql.Date sqlDate = new java.sql.Date(dt.getTime());
String datetime = sqlDate.toString();
return datetime;
} catch (Exception ee) {
ee.printStackTrace();
return null;
}
}
public static void main(String[] args) {
// TODO 自动生成方法存根
System.out.println("以字符串格式显示日期时间: "
+ new Datetime().getDatetime_String1());
System.out.println("以字符串格式显示日期时间: "
+ new Datetime().getDatetime_String2());
System.out.println("以系统格式显示日期时间: "
+ new Datetime().getDatetime_System());
System.out
.println("以中国格式显示日期时间: " + new Datetime().getDatetime_China());
System.out.println("以常用格式显示日期时间: "
+ new Datetime().getDatetime_Standard());
System.out.println("以常用格式显示日期时间: " + new Datetime().getDatetime());
System.out.println("以简单系统格式显示日期: "
+ new Datetime().getDatetime_SimpleDateFormat());
}
}