Date类型时间转换为String类型
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowTime = sdf.format(date);
String类型转换为Date类型时间
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd ");
String s = "2011-07-09 ";
Date date = sdf.parse(s);
String类型转换为Date类型时间
public static Date strToDateLong(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm");
ParsePosition pos = new ParsePosition(0);//用来标明解析开始位,其实也可以不明传pos的
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
Calender转String
Calendar calendat = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd ");
String dateStr =sdf.format(nextStart.getTime())
String 转化Calendar
String str="2012-5-27";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date =sdf.parse(str);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Calender转Date
Calendar c = Calendar.getInstance();
Date d = c.getTime();
Date转化为Calendar
Date date=new Date();
Calendar cal=Calendar.getInstance();
cal.setTime(date);
java.util.Calendar类:是一个日历类
Calendar类是一个抽象类,里边提供了很多操作日历字段的方法如:YEAR、 MONTH、 DAY OF MONTH、HOUR
1.Calendar类无法直接创建对象使用,里边有一个静态方法叫 getInstance(),getInstance()方法返回了 Calendar类的子类对象
Calendar calendar = Calendar.getInstance();使用默认时区和语言环境获得一个日历。
calender方法:
public int get(int field)
// 返回给定日历字段的值
public void set(int field, int value)
// 将给定的日历字段设置为给定值。
public abstract void add(int field, int amount)
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量。
public Date getTime()
// 返回一个表示此 Calendar时间值(从历元到现在的亳秒偏移量)的Date对象。
一、get()方法:返回给定日历字段的值
import java.util.Calendar;
public class DemoCalendarGet {
public static void main(String[] args) {
// 首先使用getInstance()方法获取calendar对象
Calendar calendar = Calendar.getInstance();
// 调用get方法获取给定日历字段的值
System.out.println(
"年:" + calendar.get(Calendar.YEAR) + "\n" +
"月:" + calendar.get(Calendar.MONTH) + "\n" +
"一个月中的某一天:" + calendar.get(Calendar.DATE) + "\n" +
"一个月中的某一天:" + calendar.get(Calendar.DAY_OF_MONTH) + "\n" +
"时:" + calendar.get(Calendar.HOUR) + "\n" +
"分:" + calendar.get(Calendar.MINUTE) + "\n" +
"秒:" + calendar.get(Calendar.SECOND)
);
}
}
二、set()方法:将给定日历字段设置为给定值
import java.util.Calendar;
public class DemoCalendarSet {
public static void main(String[] args) {
// 首先使用getInstance()方法获取calendar对象
Calendar calendar = Calendar.getInstance();
System.out.println("\n设置前");
System.out.println(
"年:" + calendar.get(Calendar.YEAR) + "\n" +
"月:" + calendar.get(Calendar.MONTH) + "\n" +
"一个月中的某一天:" + calendar.get(Calendar.DAY_OF_MONTH)
);
// 将年设置为 2008
calendar.set(Calendar.YEAR, 2008);
// 将月设置为 10
calendar.set(Calendar.MONTH, 10);
// 将日期设置为 3
calendar.set(Calendar.DAY_OF_MONTH, 3);
System.out.println("\n分别设置后");
System.out.println(
"年:" + calendar.get(Calendar.YEAR) + "\n" +
三、add()方法:根据日历规则,给定的日历字段添加或减去指定的时间量
import java.util.Calendar;
public class DemoCalendarAdd {
public static void main(String[] args) {
// 首先使用getInstance()方法获取calendar对象
Calendar calendar = Calendar.getInstance();
System.out.println("\n变化前");
System.out.println(
"年:" + calendar.get(Calendar.YEAR) + "\n" +
"月:" + calendar.get(Calendar.MONTH) + "\n" +
"一个月中的某一天:" + calendar.get(Calendar.DAY_OF_MONTH)
);
// 把年减少两年
calendar.add(Calendar.YEAR, -2);
// 把月份增加三个月
calendar.add(Calendar.MONTH, 3);
// 把日期增加10天
calendar.add(Calendar.DAY_OF_MONTH, 10);
System.out.println("\n变化后");
System.out.println(
"年:" + calendar.get(Calendar.YEAR) + "\n" +
时间工具类:
需求:输入2020输出2021,输入2020-11输出2020-12,输入2020-11-03输出2020-11-04
public String getDate(String time) throws ParseException {
String result = null;
Calendar calendar = Calendar.getInstance();
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM");
SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat hourFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int size = time.toCharArray().length;
if(size == 4) {
calendar.setTime(yearFormat.parse(time));
calendar.add(Calendar.YEAR, 1);
result = yearFormat.format(calendar.getTime());
}
else if(size == 7){
calendar.setTime(monthFormat.parse(time));
calendar.add(Calendar.MONTH,1);
result = monthFormat.format(calendar.getTime());
}
else if(size == 10) {
calendar.setTime(dayFormat.parse(time));
calendar.add(Calendar.DAY_OF_MONTH, 1);
result = dayFormat.format(calendar.getTime());
}
// else if(size == 13) {
// calendar.setTime(hourFormat.parse(time));
// calendar.add(Calendar.HOUR_OF_DAY, 1);
// result = hourFormat.format(calendar.getTime());
// }
return result;
}
需求:输入2020输出2020具体月份,输入2020-11输出2020年11月具体日,2020-11-03输出3号具体3号的24小时。
public class GetTimeUtil {
public List<String > getDateList(String time) throws ParseException {
Calendar calendar = Calendar.getInstance();
List<String> result = new ArrayList<>();
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM");
SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat hourFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int size = time.toCharArray().length;
if(size == 4){
calendar.setTime(yearFormat.parse(time));
int max = 11;
int min = 0;
for ( int i = min; i <= max; i++) {
calendar.set(Calendar.MONTH,i);
result.add(monthFormat.format(calendar.getTime()));
}
}
else if(size == 7){
calendar.setTime(monthFormat.parse(time));
int max = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int min = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
for ( int i = min; i <= max; i++) {
calendar.set(Calendar.DAY_OF_MONTH,i);
result.add(dayFormat.format(calendar.getTime()));
}
}
else if(size == 10) {
calendar.setTime(dayFormat.parse(time));
int max = calendar.getActualMaximum(Calendar.HOUR_OF_DAY);
int min = calendar.getActualMinimum(Calendar.HOUR_OF_DAY);
for (int i = min; i <= max; i++) {
calendar.set(Calendar.HOUR_OF_DAY, i);
result.add(hourFormat.format(calendar.getTime()));
}
}
return result;
}
}