Android MVVM框架搭建(二)OKHttp + Retrofit + RxJava

这篇博客介绍了如何在Android中搭建MVVM框架,结合OKHttp、Retrofit和RxJava进行网络请求。内容包括Base模块、异常处理、拦截器、网络请求服务的创建,以及如何利用Retrofit进行API接口封装,同时通过RxJava实现线程切换。还提供了日志工具类KLog的实现和网络框架的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public static final String TODAY = “今天”;

public static final String TOMORROW = “明天”;

public static final String SUNDAY = “星期日”;

public static final String MONDAY = “星期一”;

public static final String TUESDAY = “星期二”;

public static final String WEDNESDAY = “星期三”;

public static final String THURSDAY = “星期四”;

public static final String FRIDAY = “星期五”;

public static final String SATURDAY = “星期六”;

public static final String[] weekDays = {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY};

/**

  • 获取标准时间

  • @return 例如 2021-07-01 10:35:53

*/

public static String getDateTime() {

return new SimpleDateFormat(STANDARD_TIME, Locale.CHINESE).format(new Date());

}

/**

  • 获取完整时间

  • @return 例如 2021-07-01 10:37:00.748

*/

public static String getFullDateTime() {

return new SimpleDateFormat(FULL_TIME, Locale.CHINESE).format(new Date());

}

/**

  • 获取年月日(今天)

  • @return 例如 2021-07-01

*/

public static String getTheYearMonthAndDay() {

return new SimpleDateFormat(YEAR_MONTH_DAY, Locale.CHINESE).format(new Date());

}

/**

  • 获取年月日

  • @return 例如 2021年07月01号

*/

public static String getTheYearMonthAndDayCn() {

return new SimpleDateFormat(YEAR_MONTH_DAY_CN, Locale.CHINESE).format(new Date());

}

/**

  • 获取年月日

  • @param delimiter 分隔符

  • @return 例如 2021年07月01号

*/

public static String getTheYearMonthAndDayDelimiter(CharSequence delimiter) {

return new SimpleDateFormat(YEAR + delimiter + MONTH + delimiter + DAY, Locale.CHINESE).format(new Date());

}

/**

  • 获取时分秒

  • @return 例如 10:38:25

*/

public static String getHoursMinutesAndSeconds() {

return new SimpleDateFormat(HOUR_MINUTE_SECOND, Locale.CHINESE).format(new Date());

}

/**

  • 获取时分秒

  • @return 例如 10时38分50秒

*/

public static String getHoursMinutesAndSecondsCn() {

return new SimpleDateFormat(HOUR_MINUTE_SECOND_CN, Locale.CHINESE).format(new Date());

}

/**

  • 获取时分秒

  • @param delimiter 分隔符

  • @return 例如 2021/07/01

*/

public static String getHoursMinutesAndSecondsDelimiter(CharSequence delimiter) {

return new SimpleDateFormat(HOUR + delimiter + MINUTE + delimiter + SECOND, Locale.CHINESE).format(new Date());

}

/**

  • 获取年

  • @return 例如 2021

*/

public static String getYear() {

return new SimpleDateFormat(YEAR, Locale.CHINESE).format(new Date());

}

/**

  • 获取月

  • @return 例如 07

*/

public static String getMonth() {

return new SimpleDateFormat(MONTH, Locale.CHINESE).format(new Date());

}

/**

  • 获取天

  • @return 例如 01

*/

public static String getDay() {

return new SimpleDateFormat(DAY, Locale.CHINESE).format(new Date());

}

/**

  • 获取小时

  • @return 例如 10

*/

public static String getHour() {

return new SimpleDateFormat(HOUR, Locale.CHINESE).format(new Date());

}

/**

  • 获取分钟

  • @return 例如 40

*/

public static String getMinute() {

return new SimpleDateFormat(MINUTE, Locale.CHINESE).format(new Date());

}

/**

  • 获取秒

  • @return 例如 58

*/

public static String getSecond() {

return new SimpleDateFormat(SECOND, Locale.CHINESE).format(new Date());

}

/**

  • 获取毫秒

  • @return 例如 666

*/

public static String getMilliSecond() {

return new SimpleDateFormat(MILLISECOND, Locale.CHINESE).format(new Date());

}

/**

  • 获取时间戳

  • @return 例如 1625107306051

*/

public static long getTimestamp() {

return System.currentTimeMillis();

}

/**

  • 将时间转换为时间戳

  • @param time 例如 2021-07-01 10:44:11

  • @return 1625107451000

*/

public static long dateToStamp(String time) {

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(STANDARD_TIME, Locale.CHINESE);

Date date = null;

try {

date = simpleDateFormat.parse(time);

} catch (ParseException e) {

e.printStackTrace();

}

return Objects.requireNonNull(date).getTime();

}

/**

  • 将时间戳转换为时间

  • @param timeMillis 例如 1625107637084

  • @return 例如 2021-07-01 10:47:17

*/

public static String stampToDate(long timeMillis) {

return new SimpleDateFormat(STANDARD_TIME, Locale.CHINESE).format(new Date(timeMillis));

}

/**

  • 获取今天是星期几

  • @return 例如 星期四

*/

public static String getTodayOfWeek() {

Calendar cal = Calendar.getInstance();

cal.setTime(new Date());

int index = cal.get(Calendar.DAY_OF_WEEK) - 1;

if (index < 0) {

index = 0;

}

return weekDays[index];

}

/**

  • 根据输入的日期时间计算是星期几

  • @param dateTime 例如 2021-06-20

  • @return 例如 星期日

*/

public static String getWeek(String dateTime) {

Calendar cal = Calendar.getInstance();

if (“”.equals(dateTime)) {

cal.setTime(new Date(System.currentTimeMillis()));

} else {

SimpleDateFormat sdf = new SimpleDateFormat(YEAR_MONTH_DAY, Locale.getDefault());

Date date;

try {

date = sdf.parse(dateTime);

} catch (ParseException e) {

date = null;

e.printStackTrace();

}

if (date != null) {

cal.setTime(new Date(date.getTime()));

}

}

return weekDays[cal.get(Calendar.DAY_OF_WEEK) - 1];

}

/**

  • 获取输入日期的昨天

  • @param date 例如 2021-07-01

  • @return 例如 2021-06-30

*/

public static String getYesterday(Date date) {

Calendar calendar = new GregorianCalendar();

calendar.setTime(date);

calendar.add(Calendar.DATE, -1);

date = calendar.getTime();

return new SimpleDateFormat(YEAR_MONTH_DAY, Locale.getDefault()).format(date);

}

/**

  • 获取输入日期的明天

  • @param date 例如 2021-07-01

  • @return 例如 2021-07-02

*/

public static String getTomorrow(Date date) {

Calendar calendar = new GregorianCalendar();

calendar.setTime(date);

calendar.add(Calendar.DATE, +1);

date = calendar.getTime();

return new SimpleDateFormat(YEAR_MONTH_DAY, Locale.getDefault()).format(date);

}

/**

  • 根据年月日计算是星期几并与当前日期判断 非昨天、今天、明天 则以星期显示

  • @param dateTime 例如 2021-07-03

  • @return 例如 星期六

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值