Android日期选择器

时间:2022-06-13 18:09:52

1.大家先看效果图  由于代码太长太多,就不给大家截图代码了,我这里计算的是从今天起120天之内的日期,所以只有2015年,大家需要什么,可以根据最下边年月日封装的方法进行修改,

Android日期选择器

2.封装类

public class WheelView extends ScrollView {

public static final String TAG = WheelView.class.getSimpleName();

public static class OnWheelViewListener {
public void onSelected(int selectedIndex, String item) {
}
;
}

private Context context;
// private ScrollView scrollView;

private LinearLayout views;

public WheelView(Context context) {
super(context);
init(context);
}

public WheelView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}

public WheelView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}

// String[] items;
List<String> items;

private List<String> getItems() {
return items;
}

public void setItems(List<String> list) {
if (null == items) {
items = new ArrayList<String>();
}
items.clear();
items.addAll(list);

// 前面和后面补全
for (int i = 0; i < offset; i++) {
items.add(0, "");
items.add("");
}

initData();

}


public static final int OFF_SET_DEFAULT = 1;
int offset = OFF_SET_DEFAULT; // 偏移量(需要在最前面和最后面补全)

public int getOffset() {
return offset;
}

public void setOffset(int offset) {
this.offset = offset;
}

int displayItemCount; // 每页显示的数量

int selectedIndex = 1;


private void init(Context context) {
this.context = context;

// scrollView = ((ScrollView)this.getParent());
// Logger.d(TAG, "scrollview: " + scrollView);
Util.showXgtLog("parent: " + this.getParent());
// this.setOrientation(VERTICAL);
this.setVerticalScrollBarEnabled(false);

views = new LinearLayout(context);
views.setOrientation(LinearLayout.VERTICAL);
this.addView(views);

scrollerTask = new Runnable() {

public void run() {

int newY = getScrollY();
if (initialY - newY == 0) { // stopped
final int remainder = initialY % itemHeight;
final int divided = initialY / itemHeight;
// Logger.d(TAG, "initialY: " + initialY);
// Logger.d(TAG, "remainder: " + remainder + ", divided: " + divided);
if (remainder == 0) {
selectedIndex = divided + offset;

onSeletedCallBack();
} else {
if (remainder > itemHeight / 2) {
WheelView.this.post(new Runnable() {
@Override
public void run() {
WheelView.this.smoothScrollTo(0, initialY - remainder + itemHeight);
selectedIndex = divided + offset + 1;
onSeletedCallBack();
}
});
} else {
WheelView.this.post(new Runnable() {
@Override
public void run() {
WheelView.this.smoothScrollTo(0, initialY - remainder);
selectedIndex = divided + offset;
onSeletedCallBack();
}
});
}


}


} else {
initialY = getScrollY();
WheelView.this.postDelayed(scrollerTask, newCheck);
}
}
};


}

int initialY;

Runnable scrollerTask;
int newCheck = 50;

public void startScrollerTask() {

initialY = getScrollY();
this.postDelayed(scrollerTask, newCheck);
}

private void initData() {
displayItemCount = offset * 2 + 1;
if(views != null){
views.removeAllViews();
}

for (String item : items) {
views.addView(createView(item));
}

refreshItemView(0);
}

int itemHeight = 0;

private TextView createView(String item) {
TextView tv = new TextView(context);
tv.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
tv.setSingleLine(true);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
tv.setText(item);
tv.setGravity(Gravity.CENTER);
int padding = DisplayUtil.dip2px(context, 15);
tv.setPadding(padding, padding, padding, padding);
if (0 == itemHeight) {
itemHeight = AppSizeUtils.getViewH(tv);
Util.showXgtLog("itemHeight: " + itemHeight);
views.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, itemHeight * displayItemCount));
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) this.getLayoutParams();
this.setLayoutParams(new LinearLayout.LayoutParams(lp.width, itemHeight * displayItemCount));
}
return tv;
}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
refreshItemView(t);

if (t > oldt) {
// Logger.d(TAG, "向下滚动");
scrollDirection = SCROLL_DIRECTION_DOWN;
} else {
// Logger.d(TAG, "向上滚动");
scrollDirection = SCROLL_DIRECTION_UP;
}
}

private void refreshItemView(int y) {
int position = y / itemHeight + offset;
int remainder = y % itemHeight;
int divided = y / itemHeight;

if (remainder == 0) {
position = divided + offset;
} else {
if (remainder > itemHeight / 2) {
position = divided + offset + 1;
}

}

int childSize = views.getChildCount();
for (int i = 0; i < childSize; i++) {
TextView itemView = (TextView) views.getChildAt(i);
if (null == itemView) {
return;
}
if (position == i) {
itemView.setTextColor(getResources().getColor(R.color.black));
// itemView.setTextSize(R.dimen.service_detail_context_textsize);
} else {
itemView.setTextColor(Color.parseColor("#bbbbbb"));//灰色
// itemView.setTextSize(R.dimen.service_detail_context_textsize);

}
}
}

/**
* 获取选中区域的边界
*/
int[] selectedAreaBorder;

private int[] obtainSelectedAreaBorder() {
if (null == selectedAreaBorder) {
selectedAreaBorder = new int[2];
selectedAreaBorder[0] = itemHeight * offset;
selectedAreaBorder[1] = itemHeight * (offset + 1);
}
return selectedAreaBorder;
}

private int scrollDirection = -1;
private static final int SCROLL_DIRECTION_UP = 0;
private static final int SCROLL_DIRECTION_DOWN = 1;

Paint paint;
int viewWidth;

@Override
public void setBackgroundDrawable(Drawable background) {

if (viewWidth == 0) {
viewWidth = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth();
Util.showXgtLog("viewWidth: " + viewWidth);
}

if (null == paint) {
paint = new Paint();
paint.setColor(Color.parseColor("#dddddd"));
paint.setStrokeWidth(DisplayUtil.dip2px(context, 1f));
}

background = new Drawable() {
@Override
public void draw(Canvas canvas) {
canvas.drawLine(viewWidth * 1 / 6, obtainSelectedAreaBorder()[0], viewWidth * 5 / 6, obtainSelectedAreaBorder()[0], paint);
canvas.drawLine(viewWidth * 1 / 6, obtainSelectedAreaBorder()[1], viewWidth * 5 / 6, obtainSelectedAreaBorder()[1], paint);
}

@Override
public void setAlpha(int alpha) {

}

@Override
public void setColorFilter(ColorFilter cf) {

}

@Override
public int getOpacity() {
return 0;
}
};


super.setBackgroundDrawable(background);

}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
Util.showXgtLog("w: " + w + ", h: " + h + ", oldw: " + oldw + ", oldh: " + oldh);
viewWidth = w;
setBackgroundDrawable(null);
}

/**
* 选中回调
*/
private void onSeletedCallBack() {
if (null != onWheelViewListener) {
onWheelViewListener.onSelected(selectedIndex, items.get(selectedIndex));
}

}

public void setSeletion(int position) {
final int p = position;
selectedIndex = p + offset;
this.post(new Runnable() {
@Override
public void run() {
WheelView.this.smoothScrollTo(0, p * itemHeight);
}
});

}

public String getSeletedItem() {
return items.get(selectedIndex);
}

public int getSeletedIndex() {
return selectedIndex - offset;
}


@Override
public void fling(int velocityY) {
super.fling(velocityY / 3);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_UP) {

startScrollerTask();
}
return super.onTouchEvent(ev);
}

private OnWheelViewListener onWheelViewListener;

public OnWheelViewListener getOnWheelViewListener() {
return onWheelViewListener;
}

public void setOnWheelViewListener(OnWheelViewListener onWheelViewListener) {
this.onWheelViewListener = onWheelViewListener;
}

}
 
3.布局设置
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_gravity="center">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:id="@+id/background"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/apply_date_textsize"
android:gravity="center"
android:text="批量关闭"
android:layout_gravity="center"
android:textColor="@color/calendar_close_color"
android:textSize="@dimen/question_current_textsize"
/>
<TextView android:layout_width="match_parent"
android:layout_height="@dimen/msg_alert_line_height"
android:background="#cccccc"
android:layout_marginLeft="@dimen/general_small_space"
android:layout_marginRight="@dimen/general_small_space"
android:layout_marginTop="@dimen/apply_date_textsize"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="起始"
android:gravity="center"
android:layout_marginLeft="@dimen/general_small_topspace"
android:textSize="@dimen/account_default_amount_textsize"
android:textColor="@color/calendar_close_color"
/>
<cn.xgt.yuepaiforphotographer.widget.WheelView
android:id="@+id/calendar_wheel_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<cn.xgt.yuepaiforphotographer.widget.WheelView
android:id="@+id/calendar_wheel_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<cn.xgt.yuepaiforphotographer.widget.WheelView
android:id="@+id/calendar_wheel_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/general_small_topspace"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="至"
android:textSize="@dimen/question_current_textsize"
android:textColor="@color/calendar_close_color"
android:gravity="center_horizontal"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="终止"
android:gravity="center"
android:layout_marginLeft="@dimen/general_small_topspace"
android:textSize="@dimen/account_default_amount_textsize"
android:textColor="@color/calendar_close_color"
/>
<cn.xgt.yuepaiforphotographer.widget.WheelView
android:id="@+id/calendar_wheel_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<cn.xgt.yuepaiforphotographer.widget.WheelView
android:id="@+id/calendar_wheel_five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<cn.xgt.yuepaiforphotographer.widget.WheelView
android:id="@+id/calendar_wheel_six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/general_small_topspace"
/>
</LinearLayout>
<TextView android:layout_width="match_parent"
android:layout_height="@dimen/msg_alert_line_height"
android:layout_marginTop="@dimen/apply_date_textsize"
android:background="#cccccc"
/>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/cancel_textv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/dlg_cancel_save"
android:textSize="@dimen/question_normal_shape"
android:gravity="center"
android:paddingTop="@dimen/general_small_space"
android:paddingBottom="@dimen/general_small_space"/>
<TextView android:layout_width="@dimen/msg_alert_line_height"
android:layout_height="match_parent"
android:background="#cccccc"
/>
<TextView
android:id="@+id/current_textv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/dlg_current_close"
android:textSize="@dimen/question_normal_shape"
android:paddingTop="@dimen/general_small_space"
android:paddingBottom="@dimen/general_small_space"
android:gravity="center"/>

</LinearLayout>
</LinearLayout>

</FrameLayout>
Android日期选择器
4.数据的设置与计算
//批量关闭滑动选择方法
public void batchClose(){//批量关闭
List<String> yearList = new ArrayList<String>();//存放年份的数据
final List<String> monthList = new ArrayList<String>();//存放月份的数据
final List<String> monthStringList = new ArrayList<String>();//存放月份的数据
final List<String> dayList = new ArrayList<String>();//存放日的数据
final List<String> dayStringList = new ArrayList<String>();//存放日的数据

Calendar nowCal=Calendar.getInstance();
nowCal.add(Calendar.DAY_OF_YEAR,0);//90天后的日期
final int nowYear = nowCal.get(Calendar.YEAR);
final int nowMonth = (nowCal.get(Calendar.MONTH)+1);
final int nowDay = nowCal.get(Calendar.DATE);
yearEnd = nowYear+"年";
yearStart = nowYear+"年";
monthStart = nowMonth+"月";
monthEnd = nowMonth+"月";
dayStart = nowDay+"日";
dayEnd = nowDay+"日";

Calendar cal=Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR,120);//90天后的日期
final int lastYear = cal.get(Calendar.YEAR);
final int lastMonth = (cal.get(Calendar.MONTH)+1);
final int lastDay = cal.get(Calendar.DATE);

yearList = DateToolsUtils.getYears(nowYear, lastYear);
monthList.addAll(DateToolsUtils.getMonths(DateToolsUtils.getNumFromString(yearStart), nowYear, nowMonth, lastYear, lastMonth));
monthStringList.addAll(DateToolsUtils.getMonths(DateToolsUtils.getNumFromString(yearEnd),nowYear, nowMonth, lastYear, lastMonth));
dayList.addAll(DateToolsUtils.getDays(DateToolsUtils.getNumFromString(yearStart),DateToolsUtils.getNumFromString(monthStart), nowYear, nowMonth, nowDay, lastYear, lastMonth, lastDay));
dayStringList.addAll(DateToolsUtils.getDays(DateToolsUtils.getNumFromString(yearEnd),DateToolsUtils.getNumFromString(monthEnd), nowYear, nowMonth, nowDay, lastYear, lastMonth, lastDay));

View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.calendar_batch_close_layout,null);
cancelTextV = (TextView) view.findViewById(R.id.cancel_textv);
cancelTextV.setOnClickListener(this);
currentTextV = (TextView) view.findViewById(R.id.current_textv);
currentTextV.setOnClickListener(this);
if(batchState == 1){
currentTextV.setText("确认打开");
}

wheelOne = (WheelView) view.findViewById(R.id.calendar_wheel_one);
wheelTwo = (WheelView) view.findViewById(R.id.calendar_wheel_two);
wheelThree = (WheelView) view.findViewById(R.id.calendar_wheel_three);

wheelOne.setOffset(1);
wheelOne.setItems(yearList);
wheelOne.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
@Override
public void onSelected(int selectedIndex, String item) {
Util.showXgtLog("selectedIndex: " + selectedIndex + ", item: " + item);
yearStart = item+"";
monthList.clear();
dayList.clear();
monthList.addAll(DateToolsUtils.getMonths(DateToolsUtils.getNumFromString(yearStart),
nowYear,nowMonth, lastYear, lastMonth));
wheelTwo.setItems(monthList);
dayList.addAll(DateToolsUtils.getDays(DateToolsUtils.getNumFromString(yearStart),DateToolsUtils.getNumFromString(monthList.get(0)), nowYear, nowMonth, nowDay, lastYear, lastMonth, lastDay));
wheelThree.setItems(dayList);
}
});
wheelTwo.setOffset(1);

wheelTwo.setItems(monthList);
wheelTwo.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
@Override
public void onSelected(int selectedIndex, String item) {
Util.showXgtLog("selectedIndex: " + selectedIndex + ", item: " + item);
monthStart = item+"";
dayList.clear();
dayList.addAll(DateToolsUtils.getDays(DateToolsUtils.getNumFromString(yearStart),
DateToolsUtils.getNumFromString(monthStart), nowYear, nowMonth, nowDay, lastYear, lastMonth, lastDay));
wheelThree.setItems(dayList);
}
});
wheelThree.setOffset(1);

wheelThree.setItems(dayList);
wheelThree.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
@Override
public void onSelected(int selectedIndex, String item) {
Util.showXgtLog("selectedIndex: " + selectedIndex + ", item: " + item);
dayStart = item+"";
}
});
wheelFour = (WheelView) view.findViewById(R.id.calendar_wheel_four);
wheelFive = (WheelView) view.findViewById(R.id.calendar_wheel_five);
wheelSix = (WheelView) view.findViewById(R.id.calendar_wheel_six);

yearList = DateToolsUtils.getYears(nowYear, lastYear);
monthStringList.addAll(DateToolsUtils.getMonths(DateToolsUtils.getNumFromString(yearStart),nowYear, nowMonth, lastYear, lastMonth));

wheelFour.setOffset(1);
wheelFour.setItems(yearList);
wheelFour.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
@Override
public void onSelected(int selectedIndex, String item) {
Util.showXgtLog("selectedIndex: " + selectedIndex + ", item: " + item);
yearEnd = item+"";
monthStringList.clear();
dayStringList.clear();
monthStringList.addAll(DateToolsUtils.getMonths(DateToolsUtils.getNumFromString(yearEnd),nowYear, nowMonth, lastYear, lastMonth));
wheelFive.setItems(monthStringList);
dayStringList.addAll(DateToolsUtils.getDays(DateToolsUtils.getNumFromString(yearEnd),
DateToolsUtils.getNumFromString(monthStringList.get(0)), nowYear, nowMonth, nowDay, lastYear, lastMonth, lastDay));
wheelSix.setItems(dayStringList);

}
});
wheelFive.setOffset(1);
wheelFive.setItems(monthList);
wheelFive.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
@Override
public void onSelected(int selectedIndex, String item) {
Util.showXgtLog("selectedIndex: " + selectedIndex + ", item: " + item);
monthEnd = item+"";
dayStringList.clear();
dayStringList.addAll(DateToolsUtils.getDays(DateToolsUtils.getNumFromString(yearEnd),
DateToolsUtils.getNumFromString(monthEnd), nowYear, nowMonth, nowDay, lastYear, lastMonth, lastDay));
wheelSix.setItems(dayStringList);
}
});
wheelSix.setOffset(1);
wheelSix.setItems(dayStringList);
wheelSix.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
@Override
public void onSelected(int selectedIndex, String item) {
Util.showXgtLog("selectedIndex: " + selectedIndex + ", item: " + item);
dayEnd = item+"";
}
});
LinearLayout parent = (LinearLayout) view.findViewById(R.id.background);
parent.getLayoutParams().width = AppSizeUtils.getWidthPixels() / 5 * 4;
AppViewUtil.creatDiaglog(this, view);
}
//将年月日转化为毫秒 再转化为天数
private void dateChangeTime(CalendarCard calendarCard){
ArrayList<CheckableLayout> cells = calendarCard.getEnableCells();
String dateStart = wheelOne.getSeletedItem()+wheelTwo.getSeletedItem()+wheelThree.getSeletedItem();
String dateEnd = wheelFour.getSeletedItem()+wheelFive.getSeletedItem()+wheelSix.getSeletedItem();
DateFormat selectDateStart = new SimpleDateFormat("yyyy年MM月dd日");
Date dateStringStart = new Date();
Date dateStringEnd = new Date();
try {
dateStringStart = selectDateStart.parse(dateStart);
} catch (ParseException e) {
e.printStackTrace();
}
try {
dateStringEnd = selectDateStart.parse(dateEnd);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar nowDate=Calendar.getInstance();
nowDate.add(Calendar.DAY_OF_YEAR,0);//0天后的日期
String nowDateString=nowDate.get(Calendar.YEAR)+"年"+(nowDate.get(Calendar.MONTH)+1)+"月"
+nowDate.get(Calendar.DATE)+"日";

for(CheckableLayout cell:cells){
CardGridItem item = (CardGridItem) cell.getTag();
if(batchState == 0) {
if (nowDateString.equals(dateStart)) {//控件中选中日期与今天日期相等
if (cell.getState() == 1 && item.getDate().getTime().getTime() > (dateStringStart.getTime() + 24 * 60 * 60 * 1000)
&& item.getDate().getTime().getTime() <= (dateStringEnd.getTime() + 24 * 60 * 60 * 1000)) {
String dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(item.getDate().getTime());
calendarCard.removeSelectedCell(cell);
monthEdit.put(dateFormat, true);
closeDay++;
openCell.remove(cell);

}
} else {
if (cell.getState() == 1 && item.getDate().getTime().getTime() > dateStringStart.getTime()
&& item.getDate().getTime().getTime() <= (dateStringEnd.getTime() + 24 * 60 * 60 * 1000)) {
String dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(item.getDate().getTime());
calendarCard.removeSelectedCell(cell);
monthEdit.put(dateFormat, true);
closeDay++;
openCell.remove(cell);

}
}
}else if(batchState == 1){
if (nowDateString.equals(dateStart)) {//控件中选中日期与今天日期相等
if (cell.getState() == 2 && item.getDate().getTime().getTime() > (dateStringStart.getTime() + 24 * 60 * 60 * 1000)
&& item.getDate().getTime().getTime() <= (dateStringEnd.getTime() + 24 * 60 * 60 * 1000)) {
String dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(item.getDate().getTime());
calendarCard.addSelectedCell(cell);
monthEdit.put(dateFormat, true);
openDay++;
openCell.add(cell);
}
} else {
if (cell.getState() == 2 && item.getDate().getTime().getTime() > dateStringStart.getTime()
&& item.getDate().getTime().getTime() <= (dateStringEnd.getTime() + 24 * 60 * 60 * 1000)) {
String dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(item.getDate().getTime());
calendarCard.addSelectedCell(cell);
monthEdit.put(dateFormat, true);
openDay++;
openCell.add(cell);

}
}
}
}
}
5.各种年月日方法的封装
public class DateToolsUtils {

//返回每个月有多少天
private static int getCurrentMonthDay(int year, int month){

switch (month) {
case 1:
return 31;
case 2:
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
return 29;
}else{
return 28;
}
case 3:

return 31;
case 4:

return 30;
case 5:

return 31;
case 6:

return 30;
case 7:

return 31;
case 8:

return 31;
case 9:

return 30;
case 10:

return 31;
case 11:

return 30;
case 12:

return 31;
}
return 0;
}

/**
* 传入年份
* @param years
* @return
*/
public static List<String> getYears(int currentYear, int lastYear){
List<String> list = new ArrayList<String>();
if(currentYear > lastYear){
return list;
}
if(currentYear == lastYear){
list.add(lastYear+"年");
} else {
for (int i = currentYear; i <= lastYear; i++) {
list.add(i+"年");
}
}
return list;
}
/**
* 传入年份 月份
* @param years
* @return
* @throws ParseException
*/
public static List<String> getMonths(String selectYear1, int currentYear,int currentMonth, int lastYear, int lastMonth) {
List<String> list = new ArrayList<String>();
Calendar calendar1 = Calendar.getInstance();
calendar1.setTimeInMillis(System.currentTimeMillis());
int selectYear = 0;
if(selectYear1 == null || selectYear1.equals("")){
selectYear = calendar1.get(Calendar.YEAR);
}else{
selectYear = Integer.parseInt(selectYear1);
}
if(selectYear == currentYear){
if(selectYear > lastYear){
return list;
} else if (selectYear == lastYear && currentMonth > lastMonth){
return list;
} else {
if(selectYear == lastYear){
for (int i = currentMonth; i <= lastMonth; i++) {
list.add(i+"月");
}
} else {
for (int i = currentMonth; i <= 12; i++) {
list.add(i+"月");
}
}
}
}else if(selectYear > currentYear){
for (int i = 1; i <= lastMonth; i++) {
list.add(i+"月");
}
}
return list;
}

//得到天数
public static List<String> getDays(String selectYear1, String selectMonth1, int currentYear, int currentMonth, int currentDay, int lastYear, int lastMonth, int lastDay){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar1 = Calendar.getInstance();
calendar1.setTimeInMillis(System.currentTimeMillis());
int selectYear = 0;
int selectMonth = 0;
if(selectYear1 == null || selectYear1.equals("")){
selectYear = calendar1.get(Calendar.YEAR);
}else{
selectYear = Integer.parseInt(selectYear1);
}
if(selectMonth1 == null || selectMonth1.equals("")){
selectMonth = calendar1.get(Calendar.MONTH)+1;
}else{
selectMonth = Integer.parseInt(selectMonth1);
}
List<String> list = new ArrayList<String>();
if(selectYear > lastYear){
return list;
} else if (selectYear == lastYear && selectMonth > lastMonth){
return list;
} else {
if(selectYear == lastYear){
if(selectMonth == lastMonth){
for (int i = 1; i <= lastDay; i++) {
list.add(i+"日");
}
}else{
if(selectMonth == currentMonth){
for (int i = currentDay; i <= getCurrentMonthDay(selectYear, selectMonth); i++) {
list.add(i+"日");
}
}else if(selectMonth < lastMonth){
for (int i = 1; i <= getCurrentMonthDay(selectYear, selectMonth); i++) {
list.add(i+"日");
}
} else {
return list;
}
}
} else {
if(selectMonth == currentMonth){
for (int i = currentDay; i <= getCurrentMonthDay(selectYear, selectMonth); i++) {
list.add(i+"日");
}
}else if(selectMonth >= lastMonth){
for (int i = 1; i <= lastDay; i++) {
list.add(i+"日");
}
} else {
for (int i = 1; i <= getCurrentMonthDay(selectYear, selectMonth); i++) {
list.add(i+"日");
}
}
}
}
return list;
}
//得到90天之后的天数
public static Calendar getAfter120Day(long currntTime){
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(currntTime);
calendar.add(Calendar.DAY_OF_YEAR, 120);
return calendar;
}
//将数字截取出来
public static String getNumFromString(String a) {
Pattern p = Pattern.compile("[^0-9]");
Matcher m = p.matcher(a);
String result = m.replaceAll("");
return result;
}
//hashMap按照key值排序 时间
public static List<String> arrayList(List<String> list){
if(list == null){
return new ArrayList<String>();
}
List<String> list2 = new ArrayList<String>();
int[] times = new int[list.size()];
Map<Integer, String> map = new HashMap<Integer, String>();
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd-hh:MM");
for (int i = 0; i < list.size(); i++) {
try {
//得到小时数
int numHour = Integer.parseInt(list.get(i).split(":")[0]);
//得到分钟数
int numMunit = Integer.parseInt(list.get(i).split(":")[1]);
int time = numHour * 60 + numMunit;
// long time = format.parse("2000-10-10-"+list.get(i)).getTime();
Util.showXgtLog("-----转换时间-----"+format.parse("2000-10-10-"+list.get(i)).getTime());
map.put(time, list.get(i));
times[i] = time;
} catch (ParseException e) {
e.printStackTrace();
}
}
int temp;
for(int i=0;i<times.length;i++){//趟数
Util.showXgtLog("-------times-------"+times);
for(int j=0;j<times.length-i-1;j++){//比较次数
if(times[j]>times[j+1]){
temp=times[j];
times[j]=times[j+1];
times[j+1]=temp;
}
}
}
for (int i = 0; i < times.length; i++) {
list2.add(map.get(times[i]));
}
return list2;
}

}