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;
}
}
}
new Date().toLocaleString()