Skip to content

Commit

Permalink
修复因时区跳转日期崩溃的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
yannecer committed Apr 20, 2018
1 parent 3f403ff commit 1436097
Show file tree
Hide file tree
Showing 27 changed files with 353 additions and 304 deletions.
Binary file added .idea/caches/build_file_checksums.ser
Binary file not shown.
29 changes: 29 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions app/src/main/java/necer/ncalendardemo/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
import android.view.View;
import android.widget.TextView;

import com.necer.ncalendar.utils.MyLog;

import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.Months;

import java.util.ArrayList;
import java.util.List;

import necer.ncalendardemo.R;

public class MainActivity extends AppCompatActivity {
Expand All @@ -17,6 +26,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

TextView tvVersion = (TextView) findViewById(R.id.tv_version);
tvVersion.setText("版本:" + Utils.getCurrentVersion(this));

}

public void toMiui(View v) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.necer.ncalendar.listener.OnMonthCalendarChangedListener;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

import necer.ncalendardemo.R;

Expand All @@ -36,8 +37,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
monthcalendar.setDefaultSelect(false);
monthcalendar.setOnMonthCalendarChangedListener(new OnMonthCalendarChangedListener() {
@Override
public void onMonthCalendarChanged(DateTime dateTime) {
dateText.setText(dateTime.toLocalDate().toString());
public void onMonthCalendarChanged(LocalDate dateTime) {
dateText.setText(dateTime.toString());
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.necer.ncalendar.listener.OnMonthCalendarChangedListener;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

import necer.ncalendardemo.R;

Expand All @@ -33,8 +34,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
monthcalendar = (MonthCalendar) findViewById(R.id.monthcalendar);
monthcalendar.setOnMonthCalendarChangedListener(new OnMonthCalendarChangedListener() {
@Override
public void onMonthCalendarChanged(DateTime dateTime) {
dateText.setText(dateTime.toLocalDate().toString());
public void onMonthCalendarChanged(LocalDate dateTime) {
dateText.setText(dateTime.toString());
}
});

Expand All @@ -56,6 +57,6 @@ public void toToday(View view) {

public void setDate(View view) {
monthcalendar.setDate("2018-10-11");
// monthcalendar.setDate("2017-12-31");
// monthcalendar.setDate("1991-04-14");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.necer.ncalendar.utils.MyLog;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -82,11 +83,11 @@ public void run() {


@Override
public void onCalendarChanged(DateTime dateTime) {
tv_month.setText(dateTime.getMonthOfYear() + "月");
tv_date.setText(dateTime.getYear() + "年" + dateTime.getMonthOfYear() + "月" + dateTime.getDayOfMonth() + "日");
public void onCalendarChanged(LocalDate date) {
tv_month.setText(date.getMonthOfYear() + "月");
tv_date.setText(date.getYear() + "年" + date.getMonthOfYear() + "月" + date.getDayOfMonth() + "日");

MyLog.d("dateTime::" + dateTime);
MyLog.d("dateTime::" + date);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.necer.ncalendar.utils.MyLog;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -87,7 +88,7 @@ public void run() {
}

@Override
public void onCalendarChanged(DateTime dateTime) {
public void onCalendarChanged(LocalDate dateTime) {
tv_month.setText(dateTime.getMonthOfYear() + "月");
tv_date.setText(dateTime.getYear() + "年" + dateTime.getMonthOfYear() + "月" + dateTime.getDayOfMonth() + "日");
MyLog.d("dateTime::" + dateTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.necer.ncalendar.utils.MyLog;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

import necer.ncalendardemo.R;

Expand All @@ -32,7 +33,7 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,

monthCalendar.setOnMonthCalendarChangedListener(new OnMonthCalendarChangedListener() {
@Override
public void onMonthCalendarChanged(DateTime dateTime) {
public void onMonthCalendarChanged(LocalDate dateTime) {
MyLog.d("TestFragment1::;"+dateTime.toString());
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.necer.ncalendar.utils.MyLog;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

import necer.ncalendardemo.R;

Expand All @@ -31,7 +32,7 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,

monthCalendar.setOnMonthCalendarChangedListener(new OnMonthCalendarChangedListener() {
@Override
public void onMonthCalendarChanged(DateTime dateTime) {
public void onMonthCalendarChanged(LocalDate dateTime) {
MyLog.d("TestFragment2::;"+dateTime.toString());
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.necer.ncalendar.view.CalendarView;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

/**
* Created by necer on 2017/8/25.
Expand All @@ -22,11 +23,11 @@ public abstract class CalendarAdapter extends PagerAdapter {
protected int mCount;//总页数
protected int mCurr;//当前位置
protected SparseArray<CalendarView> mCalendarViews;
protected DateTime mDateTime;
protected LocalDate mDate;

public CalendarAdapter(Context mContext, int count, int curr, DateTime dateTime) {
public CalendarAdapter(Context mContext, int count, int curr, LocalDate date) {
this.mContext = mContext;
this.mDateTime = dateTime;
this.mDate = date;
this.mCurr = curr;
this.mCount = count;
mCalendarViews = new SparseArray<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.necer.ncalendar.view.MonthView;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

/**
* Created by necer on 2017/8/28.
Expand All @@ -17,8 +18,8 @@ public class MonthAdapter extends CalendarAdapter {

private OnClickMonthViewListener mOnClickMonthViewListener;

public MonthAdapter(Context mContext, int count, int curr, DateTime dateTime, OnClickMonthViewListener onClickMonthViewListener) {
super(mContext, count, curr, dateTime);
public MonthAdapter(Context mContext, int count, int curr, LocalDate date, OnClickMonthViewListener onClickMonthViewListener) {
super(mContext, count, curr, date);
this.mOnClickMonthViewListener = onClickMonthViewListener;
}

Expand All @@ -28,8 +29,8 @@ public Object instantiateItem(ViewGroup container, int position) {
MonthView nMonthView = (MonthView) mCalendarViews.get(position);
if (nMonthView == null) {
int i = position - mCurr;
DateTime dateTime = this.mDateTime.plusMonths(i);
nMonthView = new MonthView(mContext, dateTime, mOnClickMonthViewListener);
LocalDate date = this.mDate.plusMonths(i);
nMonthView = new MonthView(mContext, date, mOnClickMonthViewListener);
mCalendarViews.put(position, nMonthView);
}
container.addView(nMonthView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.necer.ncalendar.view.WeekView;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

/**
* Created by necer on 2017/8/30.
Expand All @@ -17,8 +18,8 @@ public class WeekAdapter extends CalendarAdapter {

private OnClickWeekViewListener mOnClickWeekViewListener;

public WeekAdapter(Context mContext, int count, int curr, DateTime dateTime, OnClickWeekViewListener onClickWeekViewListener) {
super(mContext, count, curr, dateTime);
public WeekAdapter(Context mContext, int count, int curr, LocalDate date, OnClickWeekViewListener onClickWeekViewListener) {
super(mContext, count, curr, date);
this.mOnClickWeekViewListener = onClickWeekViewListener;
}

Expand All @@ -28,7 +29,7 @@ public Object instantiateItem(ViewGroup container, int position) {

WeekView nWeekView = (WeekView) mCalendarViews.get(position);
if (nWeekView == null) {
nWeekView = new WeekView(mContext, mDateTime.plusDays((position - mCurr) * 7),mOnClickWeekViewListener);
nWeekView = new WeekView(mContext, mDate.plusDays((position - mCurr) * 7),mOnClickWeekViewListener);
mCalendarViews.put(position, nWeekView);
}
container.addView(mCalendarViews.get(position));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.necer.ncalendar.view.CalendarView;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -27,16 +28,16 @@
public abstract class CalendarPager extends ViewPager {

protected CalendarAdapter calendarAdapter;
protected DateTime startDateTime;
protected DateTime endDateTime;
protected LocalDate startDate;
protected LocalDate endDate;
protected int mPageSize;
protected int mCurrPage;
protected DateTime mInitialDateTime;//日历初始化datetime,即今天
protected DateTime mSelectDateTime;//当前页面选中的datetime
protected LocalDate mInitialDate;//日历初始化date,即今天
protected LocalDate mSelectDate;//当前页面选中的date
protected List<String> pointList;//圆点

protected boolean isPagerChanged = true;//是否是手动翻页
protected DateTime lastSelectDateTime;//上次选中的datetime
protected LocalDate lastSelectDate;//上次选中的date
protected boolean isDefaultSelect = true;//是否默认选中


Expand All @@ -58,10 +59,6 @@ public CalendarPager(Context context, AttributeSet attrs) {
Attrs.lunarTextSize = ta.getDimension(R.styleable.NCalendar_lunarTextSize, Utils.sp2px(context, 10));
Attrs.selectCircleRadius = ta.getDimension(R.styleable.NCalendar_selectCircleRadius, Utils.dp2px(context, 20));


MyLog.d("selectCircleRadius::" + Attrs.selectCircleRadius);


Attrs.isShowLunar = ta.getBoolean(R.styleable.NCalendar_isShowLunar, true);

Attrs.pointSize = ta.getDimension(R.styleable.NCalendar_pointSize, (int) Utils.dp2px(context, 2));
Expand Down Expand Up @@ -90,10 +87,10 @@ public CalendarPager(Context context, AttributeSet attrs) {

ta.recycle();

mInitialDateTime = new DateTime().withTimeAtStartOfDay();
mInitialDate = new LocalDate();

startDateTime = new DateTime(startString == null ? "1901-01-01" : startString);
endDateTime = new DateTime(endString == null ? "2099-12-31" : endString);
startDate = new LocalDate(startString == null ? "1901-01-01" : startString);
endDate = new LocalDate(endString == null ? "2099-12-31" : endString);

setDateInterval(null, null);

Expand All @@ -110,14 +107,13 @@ public void onGlobalLayout() {

public void setDateInterval(String startString,String endString) {
if (startString != null && !"".equals(startString)) {
startDateTime = new DateTime(startString);
startDate = new LocalDate(startString);
}
if (endString != null && !"".equals(endString)) {
endDateTime = new DateTime(endString);
endDate = new LocalDate(endString);
}


if (mInitialDateTime.getMillis() < startDateTime.getMillis() || mInitialDateTime.getMillis() > endDateTime.getMillis()) {
if (mInitialDate.isBefore(startDate) || mInitialDate.isAfter(endDate)) {
throw new RuntimeException(getResources().getString(R.string.range_date));
}

Expand Down Expand Up @@ -158,10 +154,10 @@ public void onPageScrollStateChanged(int state) {

protected abstract void initCurrentCalendarView(int position);

protected abstract void setDateTime(DateTime dateTime);
protected abstract void setDate(LocalDate date);

public void toToday() {
setDateTime(new DateTime().withTimeAtStartOfDay());
setDate(new LocalDate());
}


Expand All @@ -181,14 +177,14 @@ public void toLastPager() {

//设置日期
public void setDate(String formatDate) {
setDateTime(new DateTime(formatDate));
setDate(new LocalDate(formatDate));
}

public void setPointList(List<String> pointList) {

List<String> formatList = new ArrayList<>();
for (int i = 0; i < pointList.size(); i++) {
String format = new DateTime(pointList.get(i)).toString("yyyy-MM-dd");
String format = new LocalDate(pointList.get(i)).toString("yyyy-MM-dd");
formatList.add(format);
}

Expand Down
Loading

0 comments on commit 1436097

Please sign in to comment.