Calendar c=CalendargetInstance();
int y=cget(CalendarYEAR);
int m=cget(CalendarMONTH);
int d=cget(CalendarDATE);String date=y+"-"+m+"-"+d; Date d=javasqlDatevalueOf(time);
首先获取当前时间:
javautilDate nowdate = new javautilDate();
2/2
然后如果你想时间的格式和你想用的时间格式一致 那么就要格式化时间了SimpleDateFormat 的包在javatext包下SimpleDateFormat
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") //年月日 时分秒
String t = sdfparse(nowdate);
public static String getSysYear() {
Calendar date = CalendargetInstance();
String year = StringvalueOf(dateget(CalendarYEAR));
return year;
}
扩展资料:
import javautilDate;
import javatextSimpleDateFormat;
public class NowString {
public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
Systemoutprintln(dfformat(new Date()));// new Date()为获取当前系统时间
}
}
//如何获得系统日期,并把其转化为 String
import javautilDate;
import javatext;
public class StrDate{
public static void main(String[] args){
Date d = new Date();
//String str = dtoString();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd kk:mm:ss ");//其中yyyy-MM-dd是你要表示的格式
//可以任意组合,不限个数和次序;具体表示为:MM-month,dd-day,yyyy-year;kk-hour,mm-minute,ss-second;
String str=sdfformat(d);
Systemoutprintln("The date is : " str);
}
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdfformat(new Date());
得到的日期格式为:2008-05-14
不知道是不是你要的!你可以根据需要设置格式化类型("yyyy-MM-dd");
这个是我以前自己做的,你看看吧,希望对你有帮助
//导入包
import javaxswing;
import javautil;
import javaapplet;
import javaawt;
import javaawteventWindowAdapter;
import javaawteventWindowEvent;
import javatext;
public class MyClock extends Applet implements Runnable
{
private volatile Thread timer;
//定义变量
private int lastxs,lastys,lastxm,lastym,lastxh,lastyh;
private SimpleDateFormat formatter;
private String lastdate;
private Font clockFaceFont;
private Date currentDate;
private Color handColor;
private Color numberColor;
private int xcenter=80,ycenter=55;
public void init(){
int x,y;
lastxs=lastys=lastxm=lastym=lastxh=lastyh=0;
formatter=new SimpleDateFormat("EEE MMM dd hh:ss yyyy",LocalegetDefault());//设置时间格式
currentDate=new Date();//获取时间
lastdate=formatterformat(currentDate);
clockFaceFont=new Font("Serif",FontPLAIN,14);
handColor=ColorBLUE;
numberColor=ColordarkGray;
try{
setBackground(new Color(IntegerparseInt(getParameter("bgcolor"),16)));
}catch(NullPointerException e){
}catch(NumberFormatException e){
}
try{
handColor=new Color(IntegerparseInt(getParameter("fgcolor1"),16));
}catch(NullPointerException e){
}catch(NumberFormatException e){
}
try{
numberColor=new Color(IntegerparseInt(getParameter("fgcolor2"),16));
}catch(NullPointerException e){
}catch(NumberFormatException e){
}
resize(300,300);
}
public void update(Graphics g){
int xh,yh,xm,ym,xs,ys;
int s=0,m=10,h=10;
String today;
currentDate=new Date();
formatterapplyPattern("s");
try{
s=IntegerparseInt(formatterformat(currentDate));
}catch(NumberFormatException n){
s=0;
}
formatterapplyPattern("m");
try{
m=IntegerparseInt(formatterformat(currentDate));
}catch(NumberFormatException n){
m=10;
}
formatterapplyPattern("h");
try{
h=IntegerparseInt(formatterformat(currentDate));
}catch(NumberFormatException n){
h=10;
}
xs=(int)(Mathcos(sMathPI/30-MathPI/2)45+xcenter);
ys=(int)(Mathsin(sMathPI/30-MathPI/2)45+ycenter);
xm=(int)(Mathcos(mMathPI/30-MathPI/2)40+xcenter);
ym=(int)(Mathsin(mMathPI/30-MathPI/2)40+ycenter);
xh=(int)(Mathcos((h30+m/2)MathPI/180-MathPI/2)30+xcenter);
yh=(int)(Mathsin((h30+m/2)MathPI/180-MathPI/2)30+ycenter);
//获取在底部显示的日期
formatterapplyPattern("EEE MMM dd HH: mm:ss yyyy");
today=formatterformat(currentDate);
gsetFont(clockFaceFont);
gsetColor(getBackground());
if(xs!=lastxs||ys!=lastys)
{
gdrawLine(xcenter,ycenter,lastxs,lastys);
gdrawString(lastdate,5,125);
}
if(xm!=lastxm||ym!=lastym)
{
gdrawLine(xcenter,ycenter-1,lastxm,lastym);
gdrawLine(xcenter-1,ycenter,lastxm,lastym);
}
if(xh!=lastxh||yh!=lastyh)
{
gdrawLine(xcenter,ycenter-1,lastxh,lastyh);
gdrawLine(xcenter-1,ycenter,lastxh,lastyh);
}
gsetColor(numberColor);
gdrawString(today,5,125);
gdrawLine(xcenter,ycenter,xs,ys);
gsetColor(handColor);
gdrawLine(xcenter,ycenter-1,xm,ym);
gdrawLine(xcenter-1,ycenter,xm,ym);
gdrawLine(xcenter,ycenter-1,xh,yh);
gdrawLine(xcenter-1,ycenter,xh,yh);
lastxs=xs;lastys=ys;
lastxm=xh;lastym=ym;
lastdate=today;
}
public void paint(Graphics g){
gsetFont(clockFaceFont);
gsetColor(handColor);
gdrawArc(xcenter-50,ycenter-50,100,100,0,360);
gsetColor(numberColor);
gdrawString("9",xcenter-45,ycenter+3);
gdrawString("3",xcenter+40,ycenter+3);
gdrawString("12",xcenter-5,ycenter-37);
gdrawString("6",xcenter-3,ycenter+45);
gsetColor(numberColor);
gdrawString(lastdate,5,125);
gdrawLine(xcenter,ycenter,lastxs,lastys);
gsetColor(handColor);
gdrawLine(xcenter,ycenter-1,lastxm,lastym);
gdrawLine(xcenter-1,ycenter,lastxm,lastym);
gdrawLine(xcenter,ycenter-1,lastxh,lastyh);
gdrawLine(xcenter-1,ycenter,lastxh,lastyh);
}
public void start(){
timer=new Thread(this);
timerstart();
}
public void stop(){
timer=null;
}
public void run() {
//To change body of implemented methods use File | Settings | File Templates
Thread me=ThreadcurrentThread();
while(timer==me){
try{
ThreadcurrentThread()sleep(100);
}catch(InterruptedException e){
}
repaint();
}
}
public static void main(String[] args) {
JFrame f=new JFrame("显示时钟");
fsetSize(200,180);
MyClock mc=new MyClock();
mcinit();
mcstart();
fadd(mc);
/ faddWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
Systemexit(0);
}
});/
fsetDefaultCloseOperation(3);
fshow();
}
}
now=new Date;
year=nowgetFullYear();
month=nowgetMonth()+1;
day=nowgetDate();
hour=nowgetHours();
minute=nowgetMinutes();
second=nowgetSeconds();
ms=nowgetMilliseconds();
以上就是关于java如何获取当前系统时间需要用Date型的全部的内容,包括:java如何获取当前系统时间需要用Date型的、java怎么获取当前系统时间 毫秒数、java怎样获得系统当前的年份等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)