How to get current Date & Time in Java code?

xadave 2002-04-22 12:22:12
Thank you very much!
...全文
223 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
erinw 2002-04-24
  • 打赏
  • 举报
回复
试一试下面的程序,应该没有问题
import java.util.*;
class a
{ public static void main(String args[])
{
Date d = new Date();
System.out.println(d.getTime());
System.out.println(d.getTime()/1000/3600/24/365);
System.out.println("----------------------------");
Calendar c = Calendar.getInstance();
System.out.print(c.get(Calendar.YEAR)+" 年 " +
(c.get(Calendar.MONTH)+1)+" 月 " +
c.get(Calendar.DATE)+" 日 " );
System.out.println(c.get(Calendar.HOUR)+ " 时 " +
c.get(Calendar.MINUTE)+ " 分 " +
c.get(Calendar.SECOND)+ " 秒 ");
System.out.println(c.getTime().getTime());
c.set(2001,4,1); // 2001/5/1
System.out.println(c.get(Calendar.YEAR)+" 年 " +
(c.get(Calendar.MONTH)+1)+" 月 " +
c.get(Calendar.DATE)+" 日 " );
System.out.println(c.getTime().getTime()/1000/3600/24/365);
}
}
broze 2002-04-24
  • 打赏
  • 举报
回复
/**
*得到格式化后的系统当前日期
*@param strScheme 格式模式字符串
*@return 格式化后的系统当前时间,如果有异常产生,返回空串""
*@see java.util.SimpleDateFormat
*/
public static final String getNowDateTime(String strScheme)
{
String strReturn=null;
Date now = new Date();

try
{
SimpleDateFormat sdf = new SimpleDateFormat(strScheme);
strReturn=sdf.format(now);
}
catch(Exception e)
{
strReturn="";
}
return strReturn;
}
xadave 2002-04-24
  • 打赏
  • 举报
回复
I got a solution, share with you all:
Calendar rightNow = Calendar.getInstance();
int year,month,day,hour,minute,second;
year = rightNow.get(YEAR);
month = rightNow.get(MONTH);
day = rightNow.get(DAY_OF_MONTH);
hour = rightNow.get(HOUR_OF_DAY);
minute = rightNow.get(MINUTE);
second = rightNow.get(SECOND);

String strDate = ""+year+"-"+month+"-"+day;
String strTime = ""+hour+":"+minute+":"+second;
sexHome.create(hsid,sxid,sxname,oper,strDate,strTime);
worldheart 2002-04-22
  • 打赏
  • 举报
回复
java.lang
Code Samples Index
These code examples and other materials are subject to Sun Microsystems, Inc. Legal Terms


Getting the Current Time
There are two ways of getting the current time.

// One way
long time = System.currentTimeMillis();

// Another way
Date now = new Date();


ddtqfly 2002-04-22
  • 打赏
  • 举报
回复
public class CurrentTime {
public static String getYearToMinuteCurrentTime(){ //取得本地的系统时间格式: yymmddhhmm 共十位
StringBuffer send=new StringBuffer(10);
Calendar sendTime=Calendar.getInstance();
int minute=sendTime.get(Calendar.MINUTE);
int hour=sendTime.get(Calendar.HOUR_OF_DAY);
int day =sendTime.get(Calendar.DAY_OF_MONTH);
int month=sendTime.get(Calendar.MONTH)+1;
int year =sendTime.get(Calendar.YEAR);
if(year>=2000) year=year-2000;
else year=year-1900;
send.append(getFormatTime(year,2));
send.append(getFormatTime(month,2));
send.append(getFormatTime(day,2));
send.append(getFormatTime(hour,2));
send.append(getFormatTime(minute,2));
return send.toString();
}
public static String getYearToSecondCurrentTime(){ //取得本地的系统时间格式: yymmddhhmmss 共12位
StringBuffer send=new StringBuffer(10);
Calendar sendTime=Calendar.getInstance();
int second=sendTime.get(Calendar.SECOND);
int minute=sendTime.get(Calendar.MINUTE);
int hour=sendTime.get(Calendar.HOUR_OF_DAY);
int day =sendTime.get(Calendar.DAY_OF_MONTH);
int month=sendTime.get(Calendar.MONTH)+1;
int year =sendTime.get(Calendar.YEAR);
if(year>=2000) year=year-2000;
else year=year-1900;
send.append(getFormatTime(year,2));
send.append(getFormatTime(month,2));
send.append(getFormatTime(day,2));
send.append(getFormatTime(hour,2));
send.append(getFormatTime(minute,2));
send.append(getFormatTime(second,2));
return send.toString();
}

private static String getFormatTime(int time,int format){ //取得任意位的字符串
StringBuffer numm=new StringBuffer();
int length=String.valueOf(time).length();

if(format<length) return null;

for(int i=0 ;i<format-length ;i++){
numm.append("0");
}
numm.append(time);
return numm.toString().trim();
}

public static String getYearToMilMinuteCurrentTime(){ //取得本地的系统时间格式: yymmddhhmmssmis 共十五位
StringBuffer send=new StringBuffer(15);
Calendar sendTime=Calendar.getInstance();
int second = sendTime.get(Calendar.SECOND);
int miltime=sendTime.get(Calendar.MILLISECOND);
send.append(getYearToMinuteCurrentTime());
send.append(getFormatTime(second,2));
send.append(getFormatTime(miltime,3));
return send.toString();
}

}

你可以参考上面的代码,写自己所需的函数
mindd 2002-04-22
  • 打赏
  • 举报
回复
用下面的:
private String getCurrentTime() {
String strReturn="";
NSTimestamp currentTime = new NSTimestamp();
strReturn = currentTime.toString();
return strReturn;
}
aprim 2002-04-22
  • 打赏
  • 举报
回复
new Date();

62,614

社区成员

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

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