java怎么获取系统时间并赋值给String常量
10个回答
展开全部
你可以new Date()就是获得系统时间,
不过这个的格式跟我们平常的不一样,所以呢,还是必须要用:
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
还有一个问题,就是如果你的是win7系统,有可能时间获取不正确,这个需要你去改的,去网上搜下还是有答案的。
不过这个的格式跟我们平常的不一样,所以呢,还是必须要用:
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
还有一个问题,就是如果你的是win7系统,有可能时间获取不正确,这个需要你去改的,去网上搜下还是有答案的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
导包 import java.util.*;
主方法中写 Date date=new Date();
String riqi=date.toString();
就可以了。望采纳
主方法中写 Date date=new Date();
String riqi=date.toString();
就可以了。望采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(Calendar.getInstance().getTime());
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-09-06
展开全部
new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date());
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我把我用的获取系统时间类 给你吧 很好用的
package gavin.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class GetSystemTime {
private Calendar now = Calendar.getInstance();
private int year = now.get(Calendar.YEAR);
private int month = now.get(Calendar.MONTH) + 1;
private int day = now.get(Calendar.DAY_OF_MONTH);
private int hour = now.get(Calendar.HOUR);
private int minute = now.get(Calendar.MINUTE);
private int second = now.get(Calendar.SECOND);
public String getToday() {
String monthString = month + "";
if (month < 10)
monthString = "0" + monthString;
String dayhString = day + "";
if (day < 10)
dayhString = "0" + dayhString;
String today = this.year + monthString + dayhString;
return today;
}
public String getYearMonth() {
String today = this.year + "" + this.month;
return today;
}
public String getYera() {
return this.year + "";
}
public String getTohours() {
SimpleDateFormat setDateFormat = new SimpleDateFormat("HH:mm:ss");
String temp = setDateFormat.format(Calendar.getInstance().getTime());
return temp;
}
public String getDateTime() {
String monthString = month + "";
if (month < 10)
monthString = "0" + monthString;
String dayhString = day + "";
if (day < 10)
dayhString = "0" + dayhString;
String hourString = hour + "";
if (hour < 10)
hourString = "0" + hourString;
String minuteString = minute + "";
if (minute < 10)
minuteString = "0" + minuteString;
String secondString = second + "";
if (second < 10)
secondString = "0" + secondString;
String today = this.year + monthString + dayhString + hourString
+ minuteString + secondString;
return today;
}
}
package gavin.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class GetSystemTime {
private Calendar now = Calendar.getInstance();
private int year = now.get(Calendar.YEAR);
private int month = now.get(Calendar.MONTH) + 1;
private int day = now.get(Calendar.DAY_OF_MONTH);
private int hour = now.get(Calendar.HOUR);
private int minute = now.get(Calendar.MINUTE);
private int second = now.get(Calendar.SECOND);
public String getToday() {
String monthString = month + "";
if (month < 10)
monthString = "0" + monthString;
String dayhString = day + "";
if (day < 10)
dayhString = "0" + dayhString;
String today = this.year + monthString + dayhString;
return today;
}
public String getYearMonth() {
String today = this.year + "" + this.month;
return today;
}
public String getYera() {
return this.year + "";
}
public String getTohours() {
SimpleDateFormat setDateFormat = new SimpleDateFormat("HH:mm:ss");
String temp = setDateFormat.format(Calendar.getInstance().getTime());
return temp;
}
public String getDateTime() {
String monthString = month + "";
if (month < 10)
monthString = "0" + monthString;
String dayhString = day + "";
if (day < 10)
dayhString = "0" + dayhString;
String hourString = hour + "";
if (hour < 10)
hourString = "0" + hourString;
String minuteString = minute + "";
if (minute < 10)
minuteString = "0" + minuteString;
String secondString = second + "";
if (second < 10)
secondString = "0" + secondString;
String today = this.year + monthString + dayhString + hourString
+ minuteString + secondString;
return today;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询