123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- class LocalDateUtils {
- /**
- * 计算两个日期相差多少年
- */
- static int daysBetweenYear(DateTime a, DateTime b) {
- int v = a.millisecondsSinceEpoch - b.millisecondsSinceEpoch;
- return v ~/ 86400000 * 30 * 12;
- }
- /**
- * 计算两个日期相差多少月
- */
- static int daysBetweenMonth(DateTime a, DateTime b) {
- int v = a.millisecondsSinceEpoch - b.millisecondsSinceEpoch;
- return v ~/ 86400000 * 30;
- }
- /**
- * 计算两个日期相差多少天
- */
- static int daysBetweenDay(DateTime a, DateTime b) {
- int v = a.millisecondsSinceEpoch - b.millisecondsSinceEpoch;
- return v ~/ 86400000;
- }
- /**
- * 计算两个日期相差多少分钟
- */
- static int daysBetweenMin(DateTime a, DateTime b) {
- int v = a.millisecondsSinceEpoch - b.millisecondsSinceEpoch;
- return v ~/ 60000;
- }
- /**
- * 计算两个日期相差多少秒
- */
- static int daysBetweenSecond(DateTime a, DateTime b) {
- int v = a.millisecondsSinceEpoch - b.millisecondsSinceEpoch;
- return v ~/ 1000;
- }
- /**
- * 计算两个日期相差多少毫秒
- */
- static int daysBetweenMillSecond(DateTime a, DateTime b) {
- int v = a.millisecondsSinceEpoch - b.millisecondsSinceEpoch;
- return v;
- }
- /**
- * 获取当天(不足两位,拼0处理)
- * space 需要拼接日期的字段
- */
- static String getMMDD(DateTime dateTime, String space) {
- String month = dateTime.month.toString().length == 1
- ? "0${dateTime.month}"
- : dateTime.month.toString();
- String day = dateTime.day.toString().length == 1
- ? "0${dateTime.day}"
- : dateTime.day.toString();
- return "${month}${space}${day}";
- }
- /**
- * 获取当天(不足两位,拼0处理)
- * space 需要拼接日期的字段
- */
- static String getYYYYMMDD(DateTime dateTime, String space) {
- String year = dateTime.year.toString();
- String month = dateTime.month.toString().length == 1
- ? "0${dateTime.month}"
- : dateTime.month.toString();
- String day = dateTime.day.toString().length == 1
- ? "0${dateTime.day}"
- : dateTime.day.toString();
- return "${year}${space}${month}${space}${day}";
- }
- /**
- * 获取当天(不足两位,拼0处理)
- * space 需要拼接日期的字段
- */
- static String getYYYYMMDDHHMMSS(DateTime dateTime, String space) {
- String year = dateTime.year.toString();
- String month = dateTime.month.toString().length == 1
- ? "0${dateTime.month}"
- : dateTime.month.toString();
- String day = dateTime.day.toString().length == 1
- ? "0${dateTime.day}"
- : dateTime.day.toString();
- String hour = dateTime.hour.toString().length == 1
- ? "0${dateTime.hour}"
- : dateTime.hour.toString();
- String minute = dateTime.minute.toString().length == 1
- ? "0${dateTime.minute}"
- : dateTime.minute.toString();
- String second = dateTime.second.toString().length == 1
- ? "0${dateTime.second}"
- : dateTime.second.toString();
- return "${year}${space}${month}${space}${day} ${hour}${space}${minute}${space}${second}";
- }
- /**
- * 获取昨天
- */
- static String getYesterDayYYYYMMDD(DateTime dateTime) {
- DateTime yesterDay = new DateTime.fromMillisecondsSinceEpoch(
- dateTime.millisecondsSinceEpoch - (24 * 60 * 60 * 1000));
- return getYYYYMMDD(yesterDay, "-");
- }
- /**
- * 获取前天
- */
- static String getDayBeforeYesterdayDayYYYYMMDD(DateTime dateTime) {
- DateTime yesterDay = new DateTime.fromMillisecondsSinceEpoch(
- dateTime.millisecondsSinceEpoch - (2 * 24 * 60 * 60 * 1000));
- return getYYYYMMDD(yesterDay, "-");
- }
- /**
- * 获取明天
- */
- static String getTomorrowDayYYYYMMDD(DateTime dateTime) {
- DateTime yesterDay = new DateTime.fromMillisecondsSinceEpoch(
- dateTime.millisecondsSinceEpoch + (24 * 60 * 60 * 1000));
- return getYYYYMMDD(yesterDay, "-");
- }
- /**
- * 获取后天
- */
- static String getTomorrowAcquiredYYYYMMDD(DateTime dateTime) {
- DateTime yesterDay = new DateTime.fromMillisecondsSinceEpoch(
- dateTime.millisecondsSinceEpoch + (2 * 24 * 60 * 60 * 1000));
- return getYYYYMMDD(yesterDay, "-");
- }
- /**
- * 获取本周开始
- */
- static String getWeekFirstDayYYYYMMDD(DateTime dateTime) {
- int current = dateTime.weekday;
- DateTime firstDay = new DateTime.fromMillisecondsSinceEpoch(
- dateTime.millisecondsSinceEpoch -
- (24 * 60 * 60 * 1000 * (current - 1)));
- return getYYYYMMDD(firstDay, "-");
- }
- /**
- * 获取本周结束
- */
- static String getWeekLastDayYYYYMMDD(DateTime dateTime) {
- int current = dateTime.weekday;
- DateTime lastDay = new DateTime.fromMillisecondsSinceEpoch(
- dateTime.millisecondsSinceEpoch +
- (24 * 60 * 60 * 1000 * (7 - current)));
- return getYYYYMMDD(lastDay, "-");
- }
- /**
- * 获取上周开始
- */
- static String getLastWeekFirstDayYYYYMMDD(DateTime dateTime) {
- int current = dateTime.weekday;
- DateTime firstDay = new DateTime.fromMillisecondsSinceEpoch(
- dateTime.millisecondsSinceEpoch -
- (24 * 60 * 60 * 1000 * (current - 1)));
- DateTime day = new DateTime.fromMillisecondsSinceEpoch(
- firstDay.millisecondsSinceEpoch - (24 * 60 * 60 * 1000 * 7));
- return getYYYYMMDD(day, "-");
- }
- /**
- * 获取上周结束
- */
- static String getLastWeekLastDayYYYYMMDD(DateTime dateTime) {
- int current = dateTime.weekday;
- DateTime lastDay = new DateTime.fromMillisecondsSinceEpoch(
- dateTime.millisecondsSinceEpoch +
- (24 * 60 * 60 * 1000 * (7 - current)));
- DateTime day = new DateTime.fromMillisecondsSinceEpoch(
- lastDay.millisecondsSinceEpoch - (24 * 60 * 60 * 1000 * 7));
- return getYYYYMMDD(day, "-");
- }
- /**
- * 获取本月第一天
- */
- static String getMonthFirstDayYYYYMMDD(DateTime dateTime, String space) {
- String year = "${DateTime.now().year}";
- String month = "${DateTime.now().month}".length == 1
- ? "0${DateTime.now().month}"
- : "${DateTime.now().month}";
- return "${year}${space}${month}${space}01";
- }
- /**
- * 获取本月最后一天
- */
- static String getMonthLastDayYYYYMMDD(DateTime dateTime, String space) {
- String year = "${DateTime.now().year}";
- String month = "${DateTime.now().month}".length == 1
- ? "0${DateTime.now().month}"
- : "${DateTime.now().month}";
- int d = getDayCounts(DateTime.now().month);
- return "${year}${space}${month}${space}${d}";
- }
- /**
- * 获取周几
- */
- static String getWeekday(DateTime dateTime) {
- // if (dateTime == null) return null;
- String weekday = '星期一';
- switch (dateTime.weekday) {
- case 1:
- weekday = '星期一';
- break;
- case 2:
- weekday = '星期二';
- break;
- case 3:
- weekday = '星期三';
- break;
- case 4:
- weekday = '星期四';
- break;
- case 5:
- weekday = '星期五';
- break;
- case 6:
- weekday = '星期六';
- break;
- case 7:
- weekday = '星期日';
- break;
- default:
- break;
- }
- return weekday;
- }
- /**
- * 获取一个月有多少天
- */
- static int getDayCounts(int month) {
- int year = DateTime.now().year;
- int end = 0;
- if (month == 1) {
- end = 31;
- } else if (month == 2) {
- if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
- end = 29;
- } else {
- end = 28;
- }
- } else if (month == 3) {
- end = 31;
- } else if (month == 4) {
- end = 30;
- } else if (month == 5) {
- end = 31;
- } else if (month == 6) {
- end = 30;
- } else if (month == 7) {
- end = 31;
- } else if (month == 8) {
- end = 31;
- } else if (month == 9) {
- end = 30;
- } else if (month == 10) {
- end = 31;
- } else if (month == 11) {
- end = 30;
- } else if (month == 12) {
- end = 31;
- }
- return end;
- }
- }
|