版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/GXSeveryday/article/details/88774033
- 利用java获取当前的时间(String类型,年-月-日 时:分:秒)
//获取当前的日期
Date date = new Date();
//设置要获取的时间类型
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//获取String类型的时间
String createdate = sdf.format(date);
- 将String类型的日期转换成Data类型,然后获取年月日
package com.java.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
public class Test {
public static void main(String[] args) throws java.text.ParseException {
try {
String times = "2019-3-22";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(times);
System.out.println(date);
// 获取String字符串中的年
SimpleDateFormat y = new SimpleDateFormat("yyyy");
System.out.println(y.format(date));
// 获取String字符串中的月
SimpleDateFormat m = new SimpleDateFormat("MM");
System.out.println(m.format(date));
// 获取String字符串中的日
SimpleDateFormat d = new SimpleDateFormat("dd");
System.out.println(d.format(date));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* Date date = new Date(); SimpleDateFormat dateFormat= new
* SimpleDateFormat("yyyy-MM-dd"); System.out.println(dateFormat.format(date));
*/
}
}