Java:日期类Date与Calendar

java

Timestamp类型与日期类型之间的转化?

            Timestamp timestamp = Timestamp.valueOf("2017-03-17 07:00:00");

Date date = new Date(timestamp.getTime());

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

System.out.println(format.format(date));

Java如何字符串日期转化为日期类型?

    /**

* 字符串转换成日期

*

* @param str

* @return date

*/

public static Date strToDate(String str) {

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date date = null;

try {

date = format.parse(str);

} catch (ParseException e) {

e.printStackTrace();

}

return date;

}

怎么获取系统系统时间,通过java?

Date now=   new Date();

SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSSSSS");

String nowFormate= dateFormat.format(now);

System.out.println(nowFormate);

输出结果

2017-02-16 20:56:12.000000278

如何获取当前系统是星期?

方法一、

    String weekOfDay= getWeekOfDay(now);

System.out.println(weekOfDay);

public static String getWeekOfDay(Date date){

String[] weekDays=new String[]{"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

Calendar calendar= Calendar.getInstance();

calendar.setTime(date);

int weekIndex= calendar.get(Calendar.DAY_OF_WEEK)-1;

if(weekIndex<0)

weekIndex=0;

return weekDays[weekIndex];

}

方法二、

        SimpleDateFormat   dateFormat2=new SimpleDateFormat("EEEE");

String nowFormate2= dateFormat2.format(now);

System.out.println(nowFormate2);

Calendar获取当前系统日期的年、月、日

        Calendar calendar= Calendar.getInstance();

calendar.setTime(now);

int year= calendar.get(Calendar.YEAR);

int month= calendar.get(Calendar.MONTH)+1;

int day= calendar.get(Calendar.DAY_OF_MONTH);

System.out.println(year+"-"+month+"-"+day);

以上是 Java:日期类Date与Calendar 的全部内容, 来源链接: utcz.com/z/390948.html

回到顶部