用扫描器获取输入的时间(年月日时分),这个时间的格式是常用的格式,然后格式化这个时间,把格式化的时间输出到控制台,可以在控制台重复输入时间.格式化的时间参考企业微信聊天记录的展示时间.
代码如下:
package seven.four;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.MonthDay;
import java.time.Year;
/**
* 时间工具类
*
* @author 张林琛<br>
* @date 2019/12/06 10:36:41
* @version 1.0
*/
public class TimeTool {
/**
* 年份
*/
private static int year;
/**
* 月份
*/
private static int month;
/**
* 日份
*/
private static int day;
/**
* 小时
*/
private static int hour;
/**
* 分钟
*/
private static int minute;
/**
* 有31天的月份
*/
private static final int LARGE[] = { 1, 3, 5, 7, 8, 10, 12 };
/**
* 有30天的月份
*/
private static final int LITTLE[] = { 4, 6, 9, 11 };
/**
* 二月
*/
private static final int FEB = 2;
/**
* 处理时间
*
* @param str 传入的时间
*/
public static void handlingTime(String str) {
// 传入的时间不能为空或为null
if (str == null || str == "") {
System.out.println("输入的时间不合法,请重新输入:");
}
// 对时间进行处理,传给String类型的变量
String ing = formattingTime(str);
// 分割年月日
splitDate(ing);
boolean boo = handleDate(year, month, day);
// 判断是不是当年
if (year == Year.now().getValue()) {
// 判断是不是当月
if (month == MonthDay.now().getMonthValue()) {
// 判断是不是当日
if (day == MonthDay.now().getDayOfMonth()) {
amOfPm(hour, minute);
// 判断是不是昨天
} else if (MonthDay.now().getDayOfMonth() - day == 1) {
System.out.println("昨天");
amOfPm(hour, minute);
// 判断是一周的哪一天
} else if (MonthDay.now().getDayOfMonth() - day < 8 && MonthDay.now().getDayOfMonth() - day > 0) {
System.out.print(year + "年" + month + "月" + day + "日 " + week(year, month, day) + " ");
amOfPm(hour, minute);
} else {
System.out.print(month + "月" + day);
amOfPm(hour, minute);
}
} else if (MonthDay.now().getMonthValue() - month == 1) {
if (MonthDay.now().getDayOfMonth() - day < 8 && MonthDay.now().getDayOfMonth() - day + 30 > 0) {
System.out.print(year + "年" + month + "月" + day + "日 " + week(year, month, day) + " ");
amOfPm(hour, minute);
} else {
System.out.print(month + "月" + day + "日:");
amOfPm(hour, minute);
}
} else {
if (boo = false) {
System.out.println("日期格式不正确,请重新输入:");
} else {
System.out.print(month + "月" + day + "日:");
amOfPm(hour, minute);
}
}
} else {
if (boo == true) {
System.out.print(year + "年" + month + "月" + day + "日:");
amOfPm(hour, minute);
} else if (handleBigMonth(month) == true && month > 0 && month < 13) {
System.out.print(year + "年" + month + "月" + day + "日:");
amOfPm(hour, minute);
} else if (handleTinyMonth(month) == true && month > 0 && month < 13) {
System.out.print(year + "年" + month + "月" + day + "日:");
amOfPm(hour, minute);
} else {
System.out.println("日期输入错误,请重新输入:");
return;
}
}
}
/**
* 判断上午还是下午
*
* @param h 小时
* @param m 分钟
*/
private static void amOfPm(int h, int m) {
int zero = 0;
String colon = ":";
if (h > 0 && h < 12) {
if (m < 10) {
System.out.println("上午" + h + colon + zero + m);
} else {
System.out.println("上午" + h + colon + m);
}
} else if (h >= 12 && h < 24) {
if (m < 10) {
System.out.println("下午" + h + colon + zero + m);
} else {
System.out.println("上午" + h + colon + m);
}
} else {
System.out.println("输入时间不合法,请重新输入");
}
}
/**
* 一周的每一天
*
* @param y 年
* @param m 月
* @param d 日
* @return 星期天数
*/
private static String week(int y, int m, int d) {
if (DayOfWeek.MONDAY == LocalDate.of(y, m, d).getDayOfWeek()) {
return "周一";
} else if (DayOfWeek.TUESDAY == LocalDate.of(y, m, d).getDayOfWeek()) {
return "周二";
} else if (DayOfWeek.WEDNESDAY == LocalDate.of(y, m, d).getDayOfWeek()) {
return "周三";
} else if (DayOfWeek.THURSDAY == LocalDate.of(y, m, d).getDayOfWeek()) {
return "周四";
} else if (DayOfWeek.FRIDAY == LocalDate.of(y, m, d).getDayOfWeek()) {
return "周五";
} else if (DayOfWeek.SATURDAY == LocalDate.of(y, m, d).getDayOfWeek()) {
return "周六";
} else if (DayOfWeek.SUNDAY == LocalDate.of(y, m, d).getDayOfWeek()) {
return "周日";
}
return null;
}
/**
* 判断是不是大月
*
* @param m 传入的月份
* @return 是或者不是
*/
private static boolean handleBigMonth(int m) {
for (int i = 0; i < LARGE.length; i++) {
if (LARGE[i] == m && day > 0 && day < 32) {
return true;
}
}
return false;
}
/**
* 判断是不是小月
*
* @param m 传入的月份
* @return 是或者不是
*/
private static boolean handleTinyMonth(int m) {
for (int i = 0; i < LITTLE.length; i++) {
if (LITTLE[i] == m && day > 0 && day < 31) {
return true;
}
}
return false;
}
/**
*
* 分割日期
*
* @param str 日期
* @return 分割好的日期
*/
public static String[] delimiterDate(String str) {
if (str.contains(".")) {
String[] date = str.split("\\.");
return date;
} else if (str.contains("-")) {
String[] date = str.split("-");
return date;
} else if (str.contains("/")) {
String[] date = str.split("/");
return date;
}
return null;
}
/**
* 分割年,月,日
*
* @param str 年份,月份,日份
* @return
*/
public static void splitDate(String str) {
// 判断传入的日期是否合法
if (str.length() < 12 || str.length() > 16) {
System.out.println("日期输入长度不合法,请重新输入");
}
if (str.contains(" ")) {
String[] date = str.split("\\ ");
String[] d0 = date[0].split("\\.");
String[] d1 = date[0].split("\\/");
String[] d2 = date[0].split("\\-");
if (date[0].contains(".")) {
d0 = date[0].split("\\.");
}
if (d0[0].contains("/")) {
d0 = d1;
}
if (date[0].contains("-")) {
d0 = d2;
}
String[] date2 = date[1].split(":");
if (date.length != 2 || d0.length != 3 || date2.length != 2) {
System.out.println("时间不合法");
}
if (character(d0[0]) == true && character(d0[1]) == true
&& character(date2[0]) == true&& character(date2[1]) == true) {
// 此处一下的date[]代表数组的元素
year = Integer.valueOf(d0[0]);
month = Integer.valueOf(d0[1]);
day = Integer.valueOf(d0[2]);
hour = Integer.valueOf(date2[0]);
minute = Integer.valueOf(date2[1]);
} else {
System.out.println("日期格式有误,请重新输入...");
}
}
}
/**
* 对传入的字符进行处理
*
* @param string 传入的字符
*/
private static boolean character(String string) {
for (int i = 0; i < string.length();) {
if (string.charAt(i) >= 48 && string.charAt(i) <= 57) {
return true;
} else {
break;
}
}
return false;
}
/**
* 对日期进行处理
*
* @param year 年
* @param month 月
* @param day 日
* @return 是或者不是闰年
*/
private static boolean handleDate(int y, int m, int d) {
// 能被4整除或者能被400整除的年份
if (y % 4 == 0 || y % 400 == 0 && y % 100 != 0) {
// 是闰年,2月29天
if (FEB == m && day < 30 && 0 < d) {
return true;
} else {
return false;
}
} else {
// 不是闰年,2月28天
if (FEB == m && d < 29 && 0 < d) {
return true;
} else {
return false;
}
}
}
/**
* 对时间进行处理
*
* @param str 传入的时间
* @return 处理后的时间
*/
public static String formattingTime(String str) {
if (str == "null" || str == "") {
return "日期不合法,请重新输入:";
} else if (str.length() < 12 || str.length() > 16) {
return "日期输入长度不合法,请重新输入";
}
return str;
}
}
package seven.four;
import java.util.Scanner;
/**
* 测试时间类
* @author 张林琛<br>
* @date 2019/12/07 10:22:50
* @version 1.0
*/
public class TimeToolTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 获取扫描器
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个合法的时间:");
while (sc.hasNextLine()) {
String str = sc.nextLine();
if (str.equals("exit") || str.equals("退出")) {
System.out.println("程序结束!");
break;
} else {
TimeTool.handlingTime(str);
}
}
}
}
请输入一个合法的时间:
qwertyuiopsdfghjkl
日期输入错误,请重新输入:
1234567890456
日期输入错误,请重新输入:
qqqq-qq-qq qq:qq
日期格式有误,请重新输入...
日期输入错误,请重新输入:
1111-qq-11 11:qq
日期格式有误,请重新输入...
日期输入错误,请重新输入:
null
日期输入错误,请重新输入:
2019-12-07 11:6
上午11:06
2019-12-06 11:11
昨天
上午11:11
2019-12-05 11:11
2019年12月5日 周四 上午11:11
2000-2-28 11:11
2000年2月28日:上午11:11
2000-2-29 11:11
2000年2月29日:上午11:11
2000-2-30 11:11
日期输入错误,请重新输入:
程序虽然实现功能,但还存在BUG,后期会继续优化!
更多功能还在开发中!