日期转换:年、季度、月份
Calendar
本代码主要是用Calendar类来将起始时间和结束时间这一时间段转换为一年当中的季度。
Calendar:它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
常用的方法主要有以下几点:static Calendar getInstance():
由于其构造方法是protected的,所以无法直接创建该对象,通过此方法可以得到Calendar对象。如:Calendar cr=Calendar.getInstance();
。
public void set(int year,int month,int date,int hourofday,int minute,int second):
设置日历的年、月、日、时、分、秒。
public int get(int field):
返回给定日历字段的值。所谓字段就是年、月、日等等。
public void setTime(Date date):
使用给定的Date设置此日历的时间。Date------Calendar。
public Date getTime():
返回一个Date表示此日历的时间。Calendar-----Date。
abstract void add(int field,int amount):
按照日历的规则,给指定字段添加或减少时间量。
public long getTimeInMillies():
以毫秒为单位返回该日历的时间值。
实现
年份
private List<IppcRowNameResponse> getYearBetween(String startTime, String endTime) {
List<IppcRowNameResponse> monthList = new ArrayList<IppcRowNameResponse>();
try {
ArrayList<String> result = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 格式化为年月
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
min.setTime(sdf.parse(startTime));
min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
max.setTime(sdf.parse(endTime));
max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
Calendar curr = min;
while (curr.before(max)) {
result.add(sdf.format(curr.getTime()));
curr.add(Calendar.MONTH, 1);
}
for (String string : result) {
IppcRowNameResponse monthResponse = new IppcRowNameResponse();
Date date = sdf.parse(string);
DateFormat format1 = new SimpleDateFormat("yyyy");
DateFormat format = new SimpleDateFormat("yyyy年");
String yearS = format.format(date);
String year = format1.format(date);
int intyearMonth = Integer.parseInt(year);
monthResponse.setId(intyearMonth);
monthResponse.setMc(yearS);
monthList.add(monthResponse);
}
return monthList;
} catch (Exception e) {
e.printStackTrace();
}
return monthList;
}
季度
private static List<IppcRowNameResponse> getQuarterBetween(String startTime, String endTime) {
List<IppcRowNameResponse> monthList = new ArrayList<IppcRowNameResponse>();
try {
ArrayList<String> result = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 格式化为年月
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
min.setTime(sdf.parse(startTime));
min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
max.setTime(sdf.parse(endTime));
max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
Calendar curr = min;
while (curr.before(max)) {
result.add(sdf.format(curr.getTime()));
curr.add(Calendar.MONTH, 1);
}
for (String string : result) {
IppcRowNameResponse monthResponse = new IppcRowNameResponse();
Date date = sdf.parse(string);
DateFormat format1 = new SimpleDateFormat("MM");
DateFormat format = new SimpleDateFormat("yyyy");
String year = format.format(date);
String month = format1.format(date);
int parseInt = Integer.parseInt(month);
if (parseInt >= 1 && parseInt <= 3) {
month = "1";
} else if (parseInt >= 4 && parseInt <= 6) {
month = "2";
} else if (parseInt >= 7 && parseInt <= 9) {
month = "3";
} else {
month = "4";
}
int intyearMonth = Integer.parseInt(year + month);
monthResponse.setId(intyearMonth);
monthResponse.setMc(year + "年第" + month + "季度");
monthList.add(monthResponse);
}
return monthList;
} catch (Exception e) {
e.printStackTrace();
}
return monthList;
}
月份
private List<IppcRowNameResponse> getMonthBetween(String startTime, String endTime) {
List<IppcRowNameResponse> monthList = new ArrayList<IppcRowNameResponse>();
try {
ArrayList<String> result = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 格式化为年月
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
min.setTime(sdf.parse(startTime));
min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
max.setTime(sdf.parse(endTime));
max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
Calendar curr = min;
while (curr.before(max)) {
result.add(sdf.format(curr.getTime()));
curr.add(Calendar.MONTH, 1);
}
for (String string : result) {
IppcRowNameResponse monthResponse = new IppcRowNameResponse();
Date date = sdf.parse(string);
DateFormat format1 = new SimpleDateFormat("yyyyMM");
DateFormat format = new SimpleDateFormat("yyyy年MM月");
String month = format.format(date);
String yearMonth = format1.format(date);
int intyearMonth = Integer.parseInt(yearMonth);
monthResponse.setId(intyearMonth);
monthResponse.setMc(month);
monthList.add(monthResponse);
}
return monthList;
} catch (Exception e) {
e.printStackTrace();
}
return monthList;
}
本文地址:https://blog.csdn.net/weixin_45607513/article/details/109643532
上一篇: 狗和人的年龄问题