小编给大家分享一下Java常用时间格式转换工具类有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧! 开发过程中,经常遇到各种时间格式的转换。今天特此以博客的方
小编给大家分享一下Java常用时间格式转换工具类有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
开发过程中,经常遇到各种时间格式的转换。今天特此以博客的方式,记录开发过程中可能遇到的各种类型时间的格式转换,以工具类的方式引入,方便大家开发中使用!
public final class DataUtils{
//精确到年月日(英文) eg:2019-11-11
public static String FORMAT_LONOGRAM = "yyyy-MM-dd" ;
//精确到时分秒的完整时间(英文) eg:2010-11-11 12:12:12
public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";
//精确到毫秒完整时间(英文) eg:2019-11-11 12:12:12.55
public static String FORMAT_FULL_MILL = "yyyy-MM-dd HH:mm:ss.SSS"
//精确到年月日(中文)eg:2019年11月11日
public static String FORMAT_LONOGRAM_CN = "yyyy年MM月dd日"
//精确到时分秒的完整时间(中文)eg:2019年11月11日 12时12分12秒
public static String FORMAT_FULL_CN = "yyyy年MM月dd日 HH时mm分ss秒"
//精确到毫秒完整时间(中文)
public static String FORMAT_FULL_MILL_CN = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒"
public static String getDefaultFormat(){return FORMAT_FULL};
public static String format(Date date) {
return format(date, getDefaultFormat());
}
public static String format(Date date,String format){
String value = "";
if(date!=null){
SimpleDateFormat sdf = new SimpleDateFormat(format);
value = sdf.format(date);
}
return value;
}
public static String getNow(){return format(new Date());}
public static String getNow(String format){return format(new Date(),format);}
public static Date parse(String strDate){return parse(strDate,getDefaultFormat)};
public static Date parse(String strDate,String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);
try{
return sdf.parse(strDate);
}catch(ParseException e){
e.printStackTrace();
return null;
}
}
public static Date addMobth(Date date,int num){
Calendar= Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.YEAR,num);
return cal.getTime();
}
public static Date addMobth(Date date,int num){
Calendar= Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH,num);
return cal.getTime();
}
public static Date aDDDay(Date date, int num) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, num);
return cal.getTime();
}
public static Date addMinute(Date date, int num) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MINUTE, num);
return cal.getTime();
}
public static String getTimeStamp(){
SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_FULL_MILL);
Calendar cal = Calendar.getInstance();
return sdf.format(Calendar.getTime());
}
public static String getYear(Date date){return format(date).substring(0,4);}
public static String getYearMonth(Date date){return format(date).substring(0,7);}
public static int getHour(Date date){
Calendar calendar = Calendar.getInstrance();
calendar.setTime(date);
return calendar.get(Calendar.HOUR_OFDAY);
}
public static int countDays(String strDate,String format){
long time = Calendar.getInstance.getTime().getTime();
Calendar cal = Calendar.getInstance();
cal.setTime(parse(strDate,format));
long diff = cal.getTime().getTime();
return (int) (time/1000 - diff/1000)/3600/24;
}
public static int countDays(String strDate){return countDays(strDate,getDefaultFormat());}
public static int diffDays(Date date1, Date date2) {
if (date1 == null || date2 == null) return 0;
return (int) (Math.abs(date1.getTime() - date2.getTime()) / (60 * 60 * 24 * 1000));
}郑州人流医院哪家好 Http://mobile.zyyyzz.com/
public static int diffYear(Date year1,Date year2){return diffDays(year1,year2) / 365;}
public static int diffByDays(Date d1, Date d2) {
Date s1 = parse(format(d1, FORMAT_LONOGRAM ), FORMAT_LONOGRAM );
Date s2 = parse(format(d2, FORMAT_LONOGRAM ), FORMAT_LONOGRAM );
return diffDays(s1, s2);
}
public static List collectTimes(Date date, String[] strs) {
List result = new ArrayList<>();
List times = Arrays.asList(strs);
String dateStr = format(date, FORMAT_LONOGRAM );
String pattern = FORMAT_LONOGRAM + " k";
if (times.size() > 0) {
times.stream().forEach(t -> {
result.add(parse(dateStr + " " + t, pattern));
});
}
return result;
}
public static String getWeekOfDate(Date dt) {
String[] weekDays = {"1", "2", "3", "4", "5", "6", "7"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK);
if (0 == w) {
w = 7;
}
return weekDays[w];
}
public static String intToCn(int hourInt, String[] timeArray) {
String result = "";
if (0 <= hourInt && hourInt <= 10) {
result += timeArray[hourInt] + "\n";
} else if (11 <= hourInt && hourInt <= 19) {
result += (timeArray[10] + "\n" + timeArray[hourInt % 10]) + "\n";
} else {
result += (timeArray[hourInt / 10] + "\n" + timeArray[10] + "\n" + (hourInt % 10 == 0 ? "" : timeArray[hourInt % 10] + "\n"));
}
return result;
}
public static String hourToCn(String hour){
String[] timeArray = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"};
String[] hourArray = hour.split(":");
int hourInt = Integer.parseInt(hourArray[0]);
int minute = Integer.parseInt(hourArray[1]);
String result = intToZh(hourInt, timeArray) + "点\n" + intToZh(minute, timeArray) + "分";
return result;
}
public static LinkedHashMap dateAfterWeek(String startTime) {
LinkedHashMap result = new LinkedHashMap<>();
try {
Date date = parse(startTime,FORMAT_LONOGRAM);
for (int i = 0; i < 7; i++) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(calendar.DATE, m); //把日期往后增加一天,整数往后推,负数往前移动 时间戳转时间
Date newDate = calendar.getTime();
String str = new SimpleDateFormat("yyyy-MM-dd").format(newDate);
result.put(str, newDate);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static String[] dateAfterWeekArray(String startTime) {
String weekArray[] = new String[7];
try {
Date date = parse(startTime,FORMAT_LONOGRAM);
for (int i = 0; i < 7; i++) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(calendar.DATE, m); //把日期往后增加一天,整数往后推,负数往前移动 时间戳转时间
Date newDate = calendar.getTime();
weekArray[m] = new SimpleDateFormat("yyyy-MM-dd").format(newDate);
}
} catch (Exception e) {
e.printStackTrace();
}
return weekArray;
}
}
以上是“Java常用时间格式转换工具类有哪些”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网精选频道!
--结束END--
本文标题: Java常用时间格式转换工具类有哪些
本文链接: https://lsjlt.com/news/230142.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0