Java Calendarl类
Calendar类是一个抽象类,它提供了一些方法,用于在特定的时间瞬间和一组日历字段(如年、月、月的日、小时等)之间进行转换,并用于操作日历字段,例如获取下一周的日期。时间中的一个瞬间可以用毫秒值来表示,该毫秒值是从1970年1月1日00:00:00.000 GMT 开始的历元偏移量。
Calendar rightNow = Calendar.getInstance();
int year = rightNow.get(Calendar.YEAR);
int month = rightNow.get(Calendar.MONTH);
int day = rightNow.get(Calendar.DAY_OF_MONTH);//24小时制
int hour = rightNow.get(Calendar.HOUR); //12小时制
int noondown= rightNow.get(Calendar.AM_PM); //上下午
int minute = rightNow.get(Calendar.MINUTE);
int second = rightNow.get(Calendar.SECOND);
int millisecond = rightNow.get(Calendar.MILLISECOND);
Java 特殊格式处理 DateFormatSymbols
来自 https://blog.csdn.net/the_fool_/article/details/90264110
如将yyyyMMdd转为01MAY2019
public static final String DATE_VIP_FORMAT = "yyyyMMdd";
public static String format(Date targetDate, String formatStr){
if (targetDate == null || StringUtils.isBlank(formatStr)){
return null;
}
SimpleDateFormat format = new SimpleDateFormat(formatStr);
return format.format(targetDate);
}
public static Date parse(String date, String pattern){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
try {
return simpleDateFormat.parse(date);
} catch (ParseException e) {
//blocker解决
logger.error("parse date error for input String {}",date);
}
return null;
}
public static String formatVipDateStr(Date date) {
return format(date, DATE_VIP_FORMAT);
}
public static Date parseVipDateStr(String date) {
return parse(date, DATE_VIP_FORMAT);
}
/**
* 将01MAY2019 转换为yyyyMMdd
*/
public static String getVipStr(String date){
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMMyyyy", Locale.ENGLISH);
DateFormatSymbols dateFormatSymbols = new DateFormatSymbols();
dateFormatSymbols.setShortMonths(new String[]{"JAN", "FEB", "MAR"
, "APR", "MAY", "JUN"
, "JUL", "AUG", "SEP"
, "OCT", "NOV", "DEC"});
dateFormat.setDateFormatSymbols(dateFormatSymbols);
Date parse = dateFormat.parse(date);
return formatVipDateStr(parse);
} catch (ParseException e) {
logger.error("parse VIP date error for input String {}",date);
}
return null;
}
/**
* 将yyyyMMdd转为01MAY2019
*/
public static String getVipFmt(String dateStr){
try {
//获取date对象
Date date = parseVipDateStr(dateStr);
SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMMyyyy", Locale.ENGLISH);
DateFormatSymbols dateFormatSymbols = new DateFormatSymbols();
dateFormatSymbols.setShortMonths(new String[]{"JAN", "FEB", "MAR"
, "APR", "MAY", "JUN"
, "JUL", "AUG", "SEP"
, "OCT", "NOV", "DEC"});
dateFormat.setDateFormatSymbols(dateFormatSymbols);
String format = dateFormat.format(date);
return format;
} catch (Exception e) {
logger.error("parse VIP date error for input String {}",dateStr);
}
return null;
}
public static void main(String[] args) {
String vipStr = getVipFmt("20190503");
System.out.println(vipStr);
}