赤青1

那些我们丢失的美好!是我们永远的遗憾与记忆

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

//格式化成日期

String date="2019-12-01";
java.util.Date date2 = new SimpleDateFormat("yyyy-MM-dd").parse(date);
String now = new SimpleDateFormat("yyyy年MM月dd日").format(date2);
System.out.println(now);

 

 

/**
*
* @Description:将时间戳转换成日期
* @Title: getDateDifference
* @date 2019-09-26 12:01
* @param @param timestamep
* @param @return 参数
* @return String 返回类型
* @throws @return String
* @param timestamep
* @return
*/
public static String getDifferenceDate(long timestamep) {
String result1 = new SimpleDateFormat("yyyy-MM-dd").format(new Date(timestamep * 1000));
return result1;
}

/**
*
* @Description: 根据当前时间参数得到后面第三天的一个时间戳
* @Title: getDateDifference
* @date 2019-09-26 11:54
* @param @param date
* @param @return
* @param @throws ParseException 参数
* @return String 返回类型
* @throws @return String
* @param date
* @return
* @throws ParseException
*/
public static String getDateDifference2(String date) throws ParseException {
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
Date date2 = sdf1.parse(date);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(date2);

calendar1.add(Calendar.DAY_OF_MONTH, 3);
String format = sdf1.format(calendar1.getTime());
return getDateTimestamp(format);
}
/**
*
* @Description: 根据当前时间参数得到前面第三天的一个时间戳
* @Title: getDateDifference
* @date 2019-09-26 11:54
* @param @param date
* @param @return
* @param @throws ParseException 参数
* @return String 返回类型
* @throws @return String
* @param date
* @return
* @throws ParseException
*/
public static String getDateDifference(String date) throws ParseException {
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
Date date2 = sdf1.parse(date);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(date2);

calendar1.add(Calendar.DAY_OF_MONTH, -3);
String format = sdf1.format(calendar1.getTime());
return getDateTimestamp(format);
}

/**
*
* @Description:将字符串时间转成String类型的时间戳
* @Title: getDateDifference
* @date 2019-09-26 11:04
* @param @param date
* @param @return 参数
* @return String 返回类型
* @throws @return String
* @param date
* @return
*/
public static String getDateTimestamp(String date) {
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date2 = sdf2.parse(date);
long strLong = Long.parseLong(String.valueOf(date2.getTime() / 1000));
return String.valueOf(strLong);
} catch (ParseException e) {
e.printStackTrace();
}
return "";
}

/**
*
* @Description: 根据时间得到月份
* @Title: getSyq
* @date 2019-11-02 01:21
* @param @return 参数
* @return Integer 返回类型
* @throws
* @return Integer
* @return
* @throws ParseException
*/
public static Integer getSyq(String date1,String date2) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(date1));
int year1 = c.get(Calendar.YEAR);
int month1 = c.get(Calendar.MONTH);
c.setTime(sdf.parse(date2));
int year2 = c.get(Calendar.YEAR);
int month2 = c.get(Calendar.MONTH);

int result;
if(year1 == year2) {
result = month1 - month2;
} else {
result = 12*(year1 - year2) + month1 - month2;
}
return result;
}

posted on 2019-09-19 14:31  赤青1  阅读(472)  评论(0编辑  收藏  举报