当前位置: 技术问答>java相关
如何取日期啊?可以一次按我想要的格式取当前日期吗?如我想得到"2001-04-13"
来源: 互联网 发布时间:2015-01-12
本文导语: JAVA的日期怎么不能取一次取年月日,还要自己去组合。累呀 | import java.util.*; public class Now { public static void main(String arg[]) { /* ** on some JDK, the default TimeZone is wrong ...
JAVA的日期怎么不能取一次取年月日,还要自己去组合。累呀
|
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;
}
}
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 now=new Date();
DateFormat df=DateFormat.getDateInstance();
String myString = df.format(now);
out.println("myString="+myString);
搞丁了吧! 要不你再看看java.text.DateFormat API 文档!!
哈哈!:)
这是我这两天第三次回答这样的问题!
|
to FJY168(风雨飞扬);
从你到手的分里扣点!
out.println("myString="+myString);==》System.out.println("myString="+myString);
从你到手的分里扣点!
out.println("myString="+myString);==》System.out.println("myString="+myString);
|
把JDK 里的DEMO 的CLOCK2.JAVA 解剖一下就得了。
|
dateclass.advance(30);