目录
SimpleDateFormat(String pattern)
add(int field, int amount); 调整时间
Date类:日期类
表示特定的瞬间,精确到毫秒,1秒=1000毫秒
一,构造:
-
public Date();
无参构造,构造一个代表当前系统时间的Date对象
-
public Date(long time);
一指定毫秒创建一个代表当前系统时间的Date对象 毫秒值代表距离 标准时间(1970年01月01日 00:00:00)的毫秒值把毫秒值转换为Date对象
二,成员方法:
-
toString();
是Date类重写了Object类的toString 方法
返回值是 (星期几 月份 月份 时:分:秒 时区 年)
-
getTime();
把Date对象转换为毫秒值
返回值是当前时间距离标准时间有多少毫秒值
DateFormat类:日期格式化类
把date对象按指定格式转换为String
DateFormat是一个抽象类,不能直接使用它
一般使用SimpleDateFormat
一,构造
-
SimpleDateFormat(String pattern)
得到格式化时间
* y 年; M 年中的月份;
* w 年中的周数;
* W月份中的周数;
* D 年中的天数 ;
* d 月份中的天数 ;
* F 月份中的星期 ;
* E 星期中的天数 ;
* a Am/pm 标记;
* H 一天中的小时数(0-23);
* k 一天中的小时数(1-24);
* K am/pm 中的小时数(0-11)
* h am/pm 中的小时数(1-12)
* m 小时中的分钟数
* s 分钟中的秒数
* S 毫秒数
* z 时区 * Z 时区
二、成员方法
-
format(Date d);格式化时间
传入Date
返回已经格式化的字符串
-
parse(str);解析时间
传入需要解析的字符串
返回Date对象
demo如图1所示:输出 :
2019年01月21日,19时 39分 53秒
Mon Jan 21 19:39:53 CST 2019
Mon Jan 21 19:39:53 CST 2019
Calendar类:日历类
Calendar是一个抽象类,不能直接使用
一般使用Calender的静态方法
一,静态方法
-
getInstance()
使用:Calendar.getInstance();
二,成员方法
-
get(Calendar.YEAR);获取时间
获取Calender中的某一个字段/成员变量
static int |
getDisplayNames的样式说明符, 表示所有样式的名称,如“1月”和“1月”。 |
static int |
AM_PM字段的值表示从午夜到中午之前的一天中的一段时间。 |
static int |
对于现场数 get和 set指示是否 HOUR是前或中午之后。 |
static int |
|
protected boolean |
如果 fields[]与当前设置的时间同步,则为真。 |
static int |
|
static int |
get和 set字段编号表示该月的日期。 |
static int |
get字段编号和 set本月的日期。 |
static int |
get字段编号和 set表示一周中的日期。 |
static int |
get字段编号和 set当月的 set几的序号。 |
static int |
get和 set字段编号, set本年度的日数。 |
static int |
MONTH字段的值表示公历和朱利安日历中的第十二个月。 |
static int |
get和 set字段编号 get夏令时偏移量(以毫秒为单位)。 |
static int |
get和 set字段号表示时代,例如在儒略历中的AD或BC。 |
static int |
MONTH字段的价值表示今年第二个月在公历和朱利安日历。 |
static int |
get和 set的不同字段的数量。 |
protected int[] |
该日历的当前设置时间的日历字段值。 |
static int |
DAY_OF_WEEK字段的值表示周五。 |
static int |
get和 set字段编号, get上午或下午的小时。 |
static int |
get字段编号和 set当天的小时数。 |
protected boolean[] |
说明是否设置日历的指定日历字段的标志。 |
protected boolean |
如果那么那么 time的值是有效的。 |
static int |
MONTH字段的价值表示今年首次在公历和朱利安日历。 |
static int |
|
static int |
|
static int |
getDisplayName和 getDisplayNames相当于 LONG_FORMAT的样式说明 符 。 |
static int |
getDisplayName和 getDisplayNames的样式说明 符 , 表示用于格式的长名称。 |
static int |
一个 getDisplayName和 getDisplayNames的样式说明 符 , 表示一个独立使用的长名称,例如月份名称作为日历头。 |
static int |
|
static int |
|
static int |
get和 set字段号表示 get内的 set数。 |
static int |
get和 set字段编号表示小时内的分钟。 |
static int |
DAY_OF_WEEK字段的值表示星期一。 |
static int |
get和 set字段号表示月份。 |
static int |
getDisplayName和 getDisplayNames的样式说明 符 , 表示用于格式的窄名称。 |
static int |
getDisplayName和 getDisplayNames的样式说明 符 独立地表示一个狭义的名称。 |
static int |
|
static int |
MONTH字段的价值表示在公历和朱利安日历中的一年中的第十个月。 |
static int |
AM_PM字段的值表示从中午到午夜之前的一天中的一段时间。 |
static int |
DAY_OF_WEEK字段的值表示星期六。 |
static int |
get和 set字段编号表示分钟内的第二个。 |
static int |
|
static int |
getDisplayName和 getDisplayNames的样式说明 符 , 相当于 SHORT_FORMAT 。 |
static int |
getDisplayName和 getDisplayNames的样式说明 符 , 表示用于格式的短名称。 |
static int |
一个用于 getDisplayName和 getDisplayNames的样式说明 符 , 表示一个简单的名称,例如一个月缩写作为日历头。 |
static int |
DAY_OF_WEEK字段的值表示星期天。 |
static int |
DAY_OF_WEEK字段的值表示星期四。 |
protected long |
这个日历的当前设定时间,以1970年1月1日,格林尼治标准时间0:00:00之后的毫秒表示。 |
static int |
DAY_OF_WEEK字段的值表示周二。 |
static int |
MONTH字段的值表示一年的第十三个月。 |
static int |
DAY_OF_WEEK字段的值表示周三。 |
static int |
get和 set字段编号, set当月的周数。 |
static int |
get和 set字段编号, set本年度的周数。 |
static int |
get现场编号和 set表示年份。 |
static int |
get和 set字段编号, get GMT以毫秒为 get的原始偏移量。 |
-
add(int field, int amount); 调整时间
给定的字段添加指定的值如demo2
注意:添加天数,月份,年等等 该方法会自动计算
输出结果:
java.util.GregorianCalendar[time=1548078976051,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2019,MONTH=0,WEEK_OF_YEAR=4,WEEK_OF_MONTH=4,DAY_OF_MONTH=21,DAY_OF_YEAR=21,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=56,SECOND=16,MILLISECOND=51,ZONE_OFFSET=28800000,DST_OFFSET=0] =======================
2019年01月21日
2019年01月23日
-
set(int field, int value)设置时间
//设置时间
calendar.set(Calendar.YEAR,1000);
calendar.set(Calendar.DAY_OF_MONTH,10);
calendar.set(Calendar.MONTH,100);
System.out.println(getDate(calendar));
输出:1008年5月10日
时间类的转换
一,Calendar 转化 String
Calendar calendar= Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = sdf.format(calendar.getTime());
二,String 转化Calendar
String str="2019-1-21";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date =sdf.parse(str);
Calendar calendar = Calendar.getInstance(); calendar.setTime(date);
三,Date 转化String
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
String dateStr=sdf.format(new Date());
四,String 转化Date
String str="2019-1-21";
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date= sdf.parse(str);
五,Date 转化Calendar
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
六,Calendar转化Date
Calendar calendar = Calendar.getInstance();
Date date =calendar.getTime();
历史版本
package com.cp.cpdate;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class CpDate {
Date date = new Date();
/**
* 得到Date时钟时间
* 方法名:getClockTime
* 创建人:chenPeng
* 时间:2018年4月16日-下午7:56:04
* 手机:17673111810
* @return String
* @exception
* @since 1.0.0
*/
public long getClockTime() {
return date.getTime();
}
/**
* 传入时间格式和时间 得到毫秒时间
* 方法名:getClockTime
* 创建人:chenPeng
* 时间:2018年4月16日-下午11:02:42
* 手机:17673111810
* @param format
* @param time
* @return long
* @exception
* @since 1.0.0
*/
public long getClockTime(String format,String time) {
SimpleDateFormat bartDateFormat = new SimpleDateFormat(format);
try {
date = bartDateFormat.parse(time);
} catch (ParseException e) {
e.printStackTrace();
System.out.println("传入的格式与时间格式不符合");
}
return date.getTime();
}
/**
* 得到完整时间sql
* 方法名:Timestamp
* 创建人:chenPeng
* 时间:2018年4月16日-下午8:10:15
* 手机:17673111810
* @return String
* @exception
* @since 1.0.0
*/
public String getTimestamp() {
java.sql.Timestamp stp = new java.sql.Timestamp(date.getTime());
return stp+"";
}
/**
* 传入毫秒级得到完整时间sql
* 方法名:getTimestamp
* 创建人:chenPeng
* 时间:2018年4月16日-下午10:56:28
* 手机:17673111810
* @param clockTime
* @return String
* @exception
* @since 1.0.0
*/
public String getTimestamp(long clockTime) {
java.sql.Timestamp stp = new java.sql.Timestamp(clockTime);
return stp+"";
}
/**
* 得到格式化时间
* y 年; M 年中的月份;
* w 年中的周数;
* W月份中的周数;
* D 年中的天数 ;
* d 月份中的天数 ;
* F 月份中的星期 ;
* E 星期中的天数 ;
* a Am/pm 标记;
* H 一天中的小时数(0-23);
* k 一天中的小时数(1-24);
* K am/pm 中的小时数(0-11)
* h am/pm 中的小时数(1-12)
* m 小时中的分钟数
* s 分钟中的秒数
* S 毫秒数
* z 时区
* Z 时区
* 方法名:getTimeFormat
* 创建人:chenPeng
* 时间:2018年4月16日-下午8:27:12
* 手机:17673111810
* @param format
* @return String
* @exception
* @since 1.0.0
*/
public String getTimeFormat(String format) {
SimpleDateFormat sf = new SimpleDateFormat(format);
return sf.format(date);
}
/**
* 得到指定毫秒级时间的时间
* 方法名:getTimeFormat
* 创建人:chenPeng
* 时间:2018年4月16日-下午8:50:31
* 手机:17673111810
* @param format
* @param clockTime
* @return String
* @exception
* @since 1.0.0
*/
public String getTimeFormat(String format,long clockTime) {
SimpleDateFormat sf = new SimpleDateFormat(format);
return sf.format(clockTime);
}
/**
* 得到指定date的时间
* 方法名:getTimeFormat
* 创建人:chenPeng
* 时间:2018年4月16日-下午8:51:00
* 手机:17673111810
* @param date
* @param format
* @return String
* @exception
* @since 1.0.0
*/
public String getTimeFormat(Date date,String format) {
SimpleDateFormat sf = new SimpleDateFormat(format);
return sf.format(date);
}
/**
* 传入改变time(传入int -为倒退多少天) 与 格式
* 方法名:changeTime
* 创建人:chenPeng
* 时间:2018年4月16日-下午8:53:11
* 手机:17673111810
* @param time
* @param format
* @return String
* @exception
* @since 1.0.0
*/
public String changeTime(int time,String format) {
CpDate cpDate = new CpDate();
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.DATE, time);
return cpDate.getTimeFormat(calendar.getTime(), format);
}
}