请教大哥们如何得到包括年、月、日、时、分、秒在内的详细时间???

tyf226 2005-01-04 11:57:14
java.util.Date dd=new java.util.Date();
int year=dd.getYear()+1900;
int month=dd.getMonth()+1;
.........

我用了这种方法,但是觉得好像很笨呀,而且sun公司也不建议使用了,请问还有没有别的好的方法??

...全文
150 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lvyuanfang 2005-01-04
  • 打赏
  • 举报
回复
DateFormat.getDateInstance().format(new Date()

);
用DateFormat,new java.util.Date().toLocaleString();已经不建议用了。
wjr1982et 2005-01-04
  • 打赏
  • 举报
回复
<%!String getDate()
{return new java.util.Date().toLocaleString();
}
%>
调用这个方法就可以了!
cuilichen 2005-01-04
  • 打赏
  • 举报
回复
构造方法中传入的参数是System.currentTimeMillis();得到的结果是当前的日期。
cuilichen 2005-01-04
  • 打赏
  • 举报
回复
这是我的一个类,给你参考一下
import java.util.Calendar;
import java.util.Date;


public class MyCalendar extends Calendar {
private Calendar ca;

public MyCalendar() {
ca = Calendar.getInstance();
}

public void set(long time){
Date date=new Date(time);
ca.setTime(date);
}

public String getYear() {
String result = Integer.toString(ca.get(Calendar.YEAR));
return result;
}

public String getMonth() {
String result = "";
int k = ca.get(Calendar.MONTH) + 1;
if (k < 10) {
result = "0" + Integer.toString(k);
} else {
result = Integer.toString(k);
}
return result;
}

public String getDayOfMonth() {
String result = "";
int k = ca.get(Calendar.DAY_OF_MONTH);
if (k < 10) {
result = "0" + Integer.toString(k);
} else {
result = Integer.toString(k);
}
return result;
}

public String getDate(){
return getYear()+getMonth()+getDayOfMonth();
}

protected void computeFields() {
}

protected void computeTime() {
}

}另外小时、分、秒的方法相同。
kissdavid 2005-01-04
  • 打赏
  • 举报
回复
SimpleDateFormat timeFormat=new SimpleDateFormat();
timeFormat.applyPattern("yyyy-MM-dd HH:mm:ss");
java.util.Date date=new java.util.Date();
String rtn=timeFormat.format(date);
tom2005 2005-01-04
  • 打赏
  • 举报
回复
up
thefishwilldie 2005-01-04
  • 打赏
  • 举报
回复
楼上做法对头,new Date()不推荐了,用Calendar吧
华生豆 2005-01-04
  • 打赏
  • 举报
回复
SimpleDateFormat my_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String time = my_time.format(new Date());
jFresH_MaN 2005-01-04
  • 打赏
  • 举报
回复
Calendar
drugon 2005-01-04
  • 打赏
  • 举报
回复
java.util.Calendar类中有set()和get()方法,你要的全都有。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧