将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
public static Date strToDateLong(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
获取昨天时间
public static String getYesterday(){
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
String yesterday = new SimpleDateFormat( "yyyy-MM-dd").format(cal.getTime());
return yesterday;
}
获取两个日期之间的所有日期(yyyy-MM-dd)
public static List<String> getBetweenDates(Date begin, Date end) {
List<String> result = new ArrayList<String>();
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(begin);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
while (begin.getTime() <= end.getTime()) {
result.add(df.format(tempStart.getTime()));
tempStart.add(Calendar.DAY_OF_YEAR, 1);
begin = tempStart.getTime();
}
return result;
}
获取两个日期之间的所有日期(yyyy-MM-dd)
public static Map<String,EnvInfoRecordLowHeight> getBetweenDatesForEnvBean(Date begin, Date end) {
Map<String,EnvInfoRecordLowHeight> result = new HashMap<String,EnvInfoRecordLowHeight>();
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(begin);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
while (begin.getTime() <= end.getTime()) {
result.put(df.format(tempStart.getTime()),new EnvInfoRecordLowHeight());
tempStart.add(Calendar.DAY_OF_YEAR, 1);
begin = tempStart.getTime();
}
return result;
}
转换日期格式
-
支持:yyyy-MM-dd HH:mm:ss yyyy/MM/dd HH:mm:ss yyyyMMdd HH:mm:ss
-
支持:yyyy-MM-dd yyyy/MM/dd yyyyMMdd
-
@param strDate
-
@return
public static Date toDate(String strDate){
Date date = null;
try{
if(StrUtil.isNotEmpty(strDate)){
if(strDate.contains(":")){//带时分秒
if(strDate.contains("-")){
date = DateUtil.strToLongDate(strDate);
}else if(strDate.contains("/")){
date = DateUtil.strToLongDateXG(strDate);
}else{
date = DateUtil.strToDateymdHHmmss(strDate);
}
}else{//不带时分秒
if(strDate.contains("-")){
date = DateUtil.strToDate(strDate);
}else if(strDate.contains("/")){
date = DateUtil.strToDateXG(strDate);
}else{
date = DateUtil.strToDateymd(strDate);
}
}
}
}catch(Exception e){
}
return date;
}
将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
public static String dateToStrLong(Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(dateDate);
return dateString;
}
将长时间格式时间转换为字符串 yyyyMMddHHmmss
public static String dateToStrLong2(Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
String dateString = formatter.format(dateDate);
return dateString;
}
将短时间格式时间转换为字符串 yyyy-MM-dd
public static String dateToStr(Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(dateDate);
return dateString;
}
将短时间格式字符串转换为时间 yyyy-MM-dd
public static Date strToDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
将短时间格式字符串转换为时间 yyyy/MM/dd
public static Date strToDateXG(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
将短时间格式字符串转换为时间 yyyyMMdd
public static Date strToDateymd(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
将短时间格式字符串转换为时间 yyyyMMdd HH:mm:ss
public static Date strToDateymdHHmmss(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
将短时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
public static Date strToLongDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
将短时间格式字符串转换为时间yyyy/MM/dd HH:mm:ss
public static Date strToLongDateXG(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
得到当前年份
public static String getYear(){
Calendar date = Calendar.getInstance();
String year = String.valueOf(date.get(Calendar.YEAR));
return year;
}
得到指定时间的上个月的年
public static int getLastMonthYear(Date date){
Calendar calendar = Calendar.getInstance();//日历对象
calendar.setTime(date);
calendar.add(Calendar.MONTH, -1);//月份减一
int month = calendar.get(Calendar.MONTH)+1;
return month;
}
得到指定时间的上个月
public static int getLastMonth(Date date){
Calendar calendar = Calendar.getInstance();//日历对象
calendar.setTime(date);
calendar.add(Calendar.MONTH, -1);//月份减一
return calendar.get(Calendar.YEAR);
}
获取上月最后一天 yyyy-MM-dd HH:mm:ss
public static String getLastDay(Date date){
//获取当前时间
Calendar cal = Calendar.getInstance();
cal.setTime(date);
//下面可以设置月份,注:月份设置要减1,所以设置1月就是1-1,设置2月就是2-1,如此类推
cal.set(Calendar.MONTH, 1-1);
//调到上个月
cal.add(Calendar.MONTH, -1);
//得到一个月最最后一天日期(31/30/29/28)
int MaxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//按你的要求设置时间
cal.set( cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), MaxDay, 23, 59, 59);
//按格式输出
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(cal.getTime());
}
获取上月第一天 yyyy-MM-dd HH:mm:ss
public static String getFirstDay(Date date){
获取前一个月第一天
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(date);
calendar1.add(Calendar.MONTH, -1);
calendar1.set(Calendar.DAY_OF_MONTH,1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String firstDay = sdf.format(calendar1.getTime())+" 00:00:00";
return firstDay;
}
得到现在小时
public static String getHour() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
String hour;
hour = dateString.substring(11, 13);
return hour;
}
得到现在分钟
public static String getTime() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
String min;
min = dateString.substring(14, 16);
return min;
}
获得指定日期的前一天
public static String getDayBefore(String specifiedDay) {
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day - 1);
String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
return dayBefore;
}
获得指定日期的后一天
public static String getDayAfter(String specifiedDay) {
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day + 1);
String dayAfter = new SimpleDateFormat("yyyy-MM-dd")
.format(c.getTime());
return dayAfter;
}
计算两个日期相差的天数
public static int getDifDay(Date date1,Date date2) throws ParseException{
if(null == date1){
return 0;
}
if(null == date2){
return 0;
}
long from = date2.getTime();
long to = date1.getTime();
int days = (int) ((to - from)/(1000 * 60 * 60 * 24));
return days;
}
计算两个日期相差的天数
public static int getDifDay(String date1,String date2) throws ParseException{
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//如2016-08-10 20:40
Date fromDate = simpleFormat.parse(date2);
Date toDate = simpleFormat.parse(date1);
long from = fromDate.getTime();
long to = toDate.getTime();
int days = (int) ((to - from)/(1000 * 60 * 60 * 24));
return days;
}
获得指定日期的前N天
public static String getDayBefore(String specifiedDay,int days) {
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day - days);
String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c
.getTime());
return dayBefore;
}
获得指定日期的后N天
public static String getDayAfter(String specifiedDay, int days) {
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day + days);
String dayAfter = new SimpleDateFormat("yyyy-MM-dd")
.format(c.getTime());
return dayAfter;
}
几分钟前的时间
public static Date getBeforeForDate(int minute){
Date now = new Date();
Date now_10 = new Date(now.getTime() - minute*60*1000); //10分钟前的时间
return now_10;
}
网友评论