请问:怎么用java语言获取当前系统时间,以便自动插入数据库中?
3个回答
展开全部
1.Date() + SimpleDateFormat()
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");Date date = new Date();System.out.println(dateFormat.format(date));
2. Calender() + SimpleDateFormat()
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));
举个例子给你:
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class GetCurrentDateTime {
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//get current date time with Date()
Date date = new Date();
System.out.println(dateFormat.format(date));
//get current date time with Calendar()
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));
}
}
别忘了import
展开全部
import java.util.Calendar;
import java.util.Date();
public class gaintime {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Date() today = new Date();
System.out.println("当前时间是" + today.toString());
Calendar now = Calendar.getInstance();
int day = now.get(Calendar.DATE);
int time = now.get(Calendar.HOUR_OF_DAY);
System.out.println(time);
if (time >= 0 && time <= 6) {
time = 0;
} else if (time >= 7 && time <= 12) {
time = 1;
} else if (time >= 13 && time <= 14) {
time = 2;
} else if (time >= 15 && time < 18) {
time = 3;
} else if (time >= 19 && time <= 22) {
time = 4;
} else if (time >= 23 && time <= 24) {
time = 5;
}
switch (time) {
case 0:
System.out.println("早上好!");
break;
case 1:
System.out.println("工作时间!");
break;
case 2:
System.out.println("午安时间!");
break;
case 3:
System.out.println("工作时间!");
break;
case 4:
System.out.println("努力工作!");
break;
case 5:
System.out.println("晚安啦!");
break;
}
}
}
import java.util.Date();
public class gaintime {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Date() today = new Date();
System.out.println("当前时间是" + today.toString());
Calendar now = Calendar.getInstance();
int day = now.get(Calendar.DATE);
int time = now.get(Calendar.HOUR_OF_DAY);
System.out.println(time);
if (time >= 0 && time <= 6) {
time = 0;
} else if (time >= 7 && time <= 12) {
time = 1;
} else if (time >= 13 && time <= 14) {
time = 2;
} else if (time >= 15 && time < 18) {
time = 3;
} else if (time >= 19 && time <= 22) {
time = 4;
} else if (time >= 23 && time <= 24) {
time = 5;
}
switch (time) {
case 0:
System.out.println("早上好!");
break;
case 1:
System.out.println("工作时间!");
break;
case 2:
System.out.println("午安时间!");
break;
case 3:
System.out.println("工作时间!");
break;
case 4:
System.out.println("努力工作!");
break;
case 5:
System.out.println("晚安啦!");
break;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
new Date().toLocaleString()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询