mysql连接测试不成功的原因有哪些
335
2022-09-27
【Android -- 开源库】MaterialCalendarView 的基本使用
一、简介
Android 的 CalendarView 的 Material Design 后端端口。 目标是拥有 Material 的外观和感觉,而不是 100% 与平台的实现相同。
二、效果图
三、使用
1. 添加依赖:
allprojects { repositories { google() mavenCentral() maven { url '} jcenter() // Warning: this repository is going to shut down soon }}---------------------------------------------------------implementation 'com.github.prolificinteractive:material-calendarview:1.6.0'
2. dialog_material_calendar.xml
3. material_calendar_decorator_selected_bg.xml
四、代码
decorators/TodayDecorator.java (今天的日期的文本特殊显示(加粗、加大字号))
public class TodayDecorator implements DayViewDecorator { private final CalendarDay today; private ForegroundColorSpan span;//文本颜色 public TodayDecorator(Context mContext) { today = CalendarDay.today(); span = new ForegroundColorSpan(mContext.getResources().getColor(R.color.materialcalendar_today_border_color)); } @Override public boolean shouldDecorate(CalendarDay day) { return today.equals(day); } @Override public void decorate(DayViewFacade view) { view.addSpan(span); view.addSpan(new StyleSpan(Typeface.BOLD)); view.addSpan(new RelativeSizeSpan(1.4f)); }}
decorators/SelectedDayDecorator.java
public class SelectedDayDecorator implements DayViewDecorator { private CalendarDay selectedDate = null; public SelectedDayDecorator() { selectedDate = CalendarDay.today(); } @Override public boolean shouldDecorate(CalendarDay day) { return selectedDate != null && day.equals(selectedDate) && !day.equals(CalendarDay.today()); } @Override public void decorate(DayViewFacade view) { view.addSpan(new RelativeSizeSpan(1.4f)); view.addSpan(new StyleSpan(Typeface.BOLD)); } /** * */ public void setDate(Date date) { this.selectedDate = CalendarDay.from(date); }}
decorators/CustomDecorator.java
public class CustomDecorator implements DayViewDecorator { private final Drawable mDrawable; public CustomDecorator(Context context) { mDrawable = context.getResources().getDrawable(R.drawable.material_calendar_decorator_selected_bg); } @Override public boolean shouldDecorate(CalendarDay day) { return true; } @Override public void decorate(DayViewFacade view) { view.setSelectionDrawable(mDrawable); }}
MaterialCalendarDialog.java
DateTimeHelper.java
public class DateTimeHelper { public static final String FORMAT_24 = "yyyy-MM-dd HH:mm:ss";//24小时制 public static final String FORMAT_12 = "yyyy-MM-dd hh:mm:ss";//12小时制 public static final String FORMAT_TEXT_24 = "yyyy年MM月dd日 HH时mm分ss秒"; public static final String FORMAT_TEXT_12 = "yyyy年MM月dd日 hh时mm分ss秒"; public static final String FORMAT_DAY = "yyyy-MM-dd";//显示年月日 /** * 将未指定格式的字符串转换成日期类型 * @param date - 20151123 或者 2015/11/23 或者2015-11-23 * @return Mon Nov 23 00:00:00 GMT+08:00 2015 */ public static Date parseStringToDate(String date) throws ParseException { Date result = null; String parse = date; parse = parse.replaceFirst("^[0-9]{4}([^0-9]?)", "yyyy$1"); parse = parse.replaceFirst("^[0-9]{2}([^0-9]?)", "yy$1"); parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)", "$1MM$2"); parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}( ?)", "$1dd$2"); parse = parse.replaceFirst("( )[0-9]{1,2}([^0-9]?)", "$1HH$2"); parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)", "$1mm$2"); parse = parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)", "$1ss$2"); SimpleDateFormat format = new SimpleDateFormat(parse, Locale.CHINA); result = format.parse(date); return result; } /** * 将日期以指定格式输出 * @param date - new Date() * @param format - "yyyy-MM-dd" * @return 2015-11-23 */ public static String formatToString(Date date, String format) { try { SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA); return sdf.format(date); } catch (Exception e) { e.printStackTrace(); } return ""; } /** * 将日期格式的字符串以指定格式输出 * @param date - "2015/11/23" * @param format - "yyyy-MM-dd" * @return 2015-11-23 */ public static String formatToString(String date, String format) { try { Date dt = DateTimeHelper.parseStringToDate(date); SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA); return sdf.format(dt); } catch (ParseException e) { e.printStackTrace(); } return date; } /** * 将时间戳转化为固定格式的日期字符串 (yyyy-MM-dd HH:mm:ss) * @param time - new Date().getTime()+"" * @return 2015-11-23 14:05:20 */ public static String getStrTime(String time) { if (time.trim().equals("") || time == null) return ""; String strTime = null; time = time.substring(0, 10); SimpleDateFormat mFormat = new SimpleDateFormat(FORMAT_24,Locale.CHINA); long ltime = Long.valueOf(time); strTime = mFormat.format(new Date(ltime * 1000L)); return strTime; } /** * 将时间戳转化为指定格式日期的字符串 * @param time - new Date().getTime()+"" * @param format - "yyyy年MM月dd日 hh时mm分ss秒" * @return 2015年11月23日 02时05分36秒 */ public static String getStrTime(String time, String format) { if (time.trim().equals("") || time == null || time.equals("null")) return ""; String strTime = null; time = time.substring(0, 10); SimpleDateFormat mFormat = new SimpleDateFormat(format, Locale.CHINA); long ltime = Long.valueOf(time); strTime = mFormat.format(new Date(ltime * 1000L)); return strTime; } /** * 当前时间提前一个月 * @return 2015-10-23 */ public static String getPerMonthDate(){ Date date = new Date();//当前日期 SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DAY,Locale.CHINA);//格式化对象 Calendar calendar = Calendar.getInstance();//日历对象 calendar.setTime(date); //设置当前日期 calendar.add(Calendar.MONTH, -1); //月份减一 return sdf.format(calendar.getTime()); } /** * 当前时间延后一个月 * @return 2015-12-23 */ public static String getLastMonthDate(){ Date date = new Date();//当前日期 SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DAY,Locale.CHINA);//格式化对象 Calendar calendar = Calendar.getInstance();//日历对象 calendar.setTime(date); //设置当前日期 calendar.add(Calendar.MONTH, 1); //月份加一 return sdf.format(calendar.getTime()); } /** * 计算时间差(单位:天) * @param startDate - "2015-11-23" * @param endDate - "2015-12-20" * @return 27(天) */ public static String getTimeDifferenceDate(String startDate, String endDate) { try { Date d1 = parseStringToDate(endDate); Date d2 = parseStringToDate(startDate); long diff = d1.getTime() - d2.getTime(); long days = diff / (1000 * 60 * 60 * 24); return days + ""; } catch (Exception e) { } return ""; } /** * 计算两个日期型的时间相差多少时间 * @param startDate - DateTimeHelper.parseStringToDate("2015-11-23") * @param endDate - DateTimeHelper.parseStringToDate("2015-12-20") * @return 3周前 */ public static String twoDateDistance(Date startDate, Date endDate) { if (startDate == null || endDate == null) { return null; } long timeLong = endDate.getTime() - startDate.getTime(); if (timeLong < 60 * 1000l) return timeLong / 1000 + "秒前"; else if (timeLong < 60 * 60 * 1000l) { timeLong = timeLong / 1000 / 60; return timeLong + "分钟前"; } else if (timeLong < 24 * 60 * 60 * 1000l) { timeLong = timeLong / 60 / 60 / 1000; return timeLong + "小时前"; } else if (timeLong < 7 * 24 * 60 * 60 * 1000l) { timeLong = timeLong / 1000 / 60 / 60 / 24; return timeLong + "天前"; } else if (timeLong < 4 * 7 * 24 * 60 * 60 * 1000l) { timeLong = timeLong / 1000 / 60 / 60 / 24 / 7; return timeLong + "周前"; } else { SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DAY,Locale.CHINA); sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00")); return sdf.format(startDate); } } /** * 判断两个日期的大小 * * @param DATE1 -- "2015-11-23 * @param DATE2 --"2015-12-20" * @return true 默认第一个比第二个时间小,所以如果第一个大于第二个,返回false */ public static boolean compare_date(String DATE1, String DATE2) { //DateFormat df = new SimpleDateFormat(); //getDateInstance方法——获取日期格式器 2015-11-23 //getDateTimeInstance方法——获取日期/时间格式器 2015-11-23 09:37:50 //getInstance方法用于获取为日期和时间使用SHORT风格的默认日期/时间格式器 DateFormat df = DateFormat.getDateInstance(); try { Date dt1 = parseStringToDate(DATE1); Date dt2 = parseStringToDate(DATE2); if (dt1.getTime() >= dt2.getTime()) { return false; } else if (dt1.getTime() < dt2.getTime()) { return true; } } catch (Exception exception) { exception.printStackTrace(); } return false; } /** * 将时间time字符串转化为Date对象 * @param strTime - 1448208000000 * @return Mon Nov 23 00:00:00 GMT+08:00 2015 */ public static Date parseFormatTimeToDate(String strTime) { Date date = new Date(); try{ date.setTime(Long.parseLong(strTime)); } catch(NumberFormatException nfe){ nfe.printStackTrace(); } return date; } /** * 获取格式化后Date字符串的Time值 * @param dateStr 20151123 或者 2015/11/23 或者2015-11-23 * @return 1448208000000 * */ public static long getParseDateTime(String dateStr){ long daterTime = 0; try { Date dt1 = parseStringToDate(dateStr); daterTime = dt1.getTime(); } catch (Exception exception) { exception.printStackTrace(); } return daterTime; } /** * 当前时间延后一个星期 * @param startDate 2016-03-16 * @return 2015-03-23 */ public static String getLastWeekDate(String startDate){ String endDate = ""; try { Date date = parseStringToDate(startDate); long startTime = date.getTime(); long endTime = startTime + 7 * 24 * 60 * 60 * 1000; endDate = getStrTime(endTime+"",FORMAT_DAY); } catch (Exception e) { } return endDate; } /** * 判断是否同一天【一般用来判断是否是今天】 * @param date new Date() * @param sameDate DateTimeHelper.parseStringToDate("2015-12-20") * @return boolean false */ public static boolean isSameDay(Date date, Date sameDate) { if (null == date || null == sameDate) { return false; } Calendar nowCalendar = Calendar.getInstance(); nowCalendar.setTime(sameDate); Calendar dateCalendar = Calendar.getInstance(); dateCalendar.setTime(date); if (nowCalendar.get(Calendar.YEAR) == dateCalendar.get(Calendar.YEAR) && nowCalendar.get(Calendar.MONTH) == dateCalendar.get(Calendar.MONTH) && nowCalendar.get(Calendar.DATE) == dateCalendar.get(Calendar.DATE)) { return true; } return false; }}
MainActivity.java
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~