对输入的日期时间进行格式化
要求:
用扫描器获取输入的时间(年月日时分),这个时间的格式是常用的格式,然后格式化这个时间,把格式化的时间输出到控制台,可以在控制台重复输入时间.格式化的时间参考企业微信聊天记录的展示时间.
练习实现代码:
package hrkj.chapter7.date$calendar;
import java.util.Scanner;
/**
* 输入日期时间 <br>
* 2019年12月5日下午5:09:01
*
* @author wcf
* @version 1.0
*/
public class DateTimeTest {
/**
* 程序入口
*
* @param args
*/
public static void main(String[] args) {
System.out.println("输入日期时间: (格式:xxxx-xx-xx xx:xx)");
System.out.println("例如: 2000-11-11 11:11");
System.out.println("------------------------------");
System.out.print("请输入: ");
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextLine()) {
String str = scanner.nextLine();
if (str.equals("exit")||str.equals("退出")) {
System.out.println("退出成功");
break;
}
//将输入的时间进行格式化
String dateTime = DateTimeTools.dateTimeFormat(str);
//如果出错则重新输入
if (dateTime.equals(DateTimeTools.DATE_ERROR)||dateTime.equals(DateTimeTools.TIME_ERROR)) {
System.out.println(dateTime+", 请重新输入");
continue;
}
//打印格式化结果
System.out.println(dateTime);
}
}
}
对输入的日期时间进行处理的类:
package hrkj.chapter7.date$calendar;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.MonthDay;
import java.time.Year;
import java.util.Arrays;
/**
* 对输入的日期,时间进行判断 <br>
* 2019年12月5日下午5:21:10
*
* @author wcf
* @version 1.0
*/
public class DateTimeTools {
/**
* 时间错误
*/
static String TIME_ERROR = "时间错误";
/**
* 日期错误
*/
static String DATE_ERROR = "日期错误";
/**
* 当前的年
*/
private static int currentYear = Year.now().getValue();
/**
* 当前的月
*/
private static int currentMonth = MonthDay.now().getMonthValue();
/**
* 当前的日
*/
private static int currentDay = MonthDay.now().getDayOfMonth();
/**
* 大月
*/
private static int[] bigMonth = { 1, 3, 5, 7, 8, 10, 12 };
/**
* 小月
*/
private static int[] smallMonth = { 4, 6, 9, 11 };
/**
* 对输入的时间进行处理
*
* @param string 输入的时间
* @return 处理的结果
*/
public static String dateTimeFormat(String string) {
String dateTime = null;
if (string == null || string.equals("") || !string.contains(" ") || !string.contains(":")) {
dateTime = TIME_ERROR;
} else if (string.length() < 12 || string.length() > 16) {
dateTime = TIME_ERROR;
} else {
// 使用空格进行分割
String[] dateTimeTmp = string.split(" ");
if (dateTimeTmp.length != 2) {
dateTime = TIME_ERROR;
} else {
// 对日期和时间分别进行判断
String date = dateHandle(dateTimeTmp[0]);
String time = timeHandle(dateTimeTmp[1]);
// 日期错误,则返回错误
if (date.equals(DATE_ERROR)) {
return date;
}
// 时间错误,则返回错误
if (time.equals(TIME_ERROR)) {
return time;
}
// 如果都设置成功,则对日期时间格式化
dateTime = (date.equals("今天") ? "" : date + " ") + time;
}
}
// 返回格式化后的日期时间
return dateTime;
}
/**
* 处理日期
*
* @param string 日期
* @return 日期处理的结果
*/
private static String dateHandle(String string) {
String result = null;
// 使用分割日期的方法对日期进行分割
String[] date = dateSplit(string);
if (date == null || date.length != 3) {
return DATE_ERROR;
} else if (!equalsNum(date[0]) || !equalsNum(date[1]) || !equalsNum(date[2])) {
return DATE_ERROR;
} else {
// 将年月日转换出来
int year = Integer.valueOf(date[0]);
int month = Integer.valueOf(date[1]);
int day = Integer.valueOf(date[2]);
// 对年月日的长度进行判定
boolean yearLength = (date[0].length() > 0 && date[0].length() == 4);
boolean monthLength = (date[1].length() > 0 && date[1].length() <= 2);
boolean dayLength = (date[1].length() > 0 && date[1].length() <= 2);
// 如果年月日的长度不符合,则提示错误
if (yearLength == false || monthLength == false || dayLength == false) {
return DATE_ERROR;
} else {
result = month + "月" + day + "日";
}
// 年份大于1970年,月份在1-12之间,天份在1-31之间
if (year > 1970 && month > 0 && month <= 12 && day > 0 && day <= 31) {
// 对月份进行判断大月小月
if (Arrays.binarySearch(bigMonth, month) >= 0 && day > 31) {
return DATE_ERROR;
} else if (Arrays.binarySearch(smallMonth, month) >= 0 && day > 30) {
return DATE_ERROR;
} else if (month == 2) {
// 判断平年,闰年
if (year % 4 == 0 || year % 400 == 0) {
if (day > 0 && day <= 29) {
result = month + "月" + day + "日";
} else {
return DATE_ERROR;
}
} else {
if (day > 0 && day <= 28) {
result = month + "月" + day + "日";
} else {
return DATE_ERROR;
}
}
}
// 同一年
if (year == currentYear) {
// 同一月
if (month == currentMonth) {
// 判断是哪一天
result = someday(year, month, day, currentDay);
}
// 下一月
else if (month - currentMonth == 1) {
// 对下个月日期进行判定
result = dayToNextMonth(year, month, day, currentMonth, currentDay);
}
// 上一月
else if (month - currentMonth == -1) {
// 对上个月日期进行判定
result = dayToLastMonth(year, month, day, currentMonth, currentDay);
} else {
// 不同月
result = month + "月" + day + "日";
}
} else {
// 不同年
result = year + "年" + month + "月" + day + "日";
}
} else {
return DATE_ERROR;
}
}
return result;
}
/**
* 对上个月时间进行转换
*
* @param year 输入的年
* @param month 输入的月
* @param day 输入的天
* @param currentMonth 当前月
* @param currentDay 当前天
* @return 转换结果
*/
private static String dayToLastMonth(int year, int month, int day, int currentMonth, int currentDay) {
String result = null;
if (currentDay == 1) {
if (Arrays.binarySearch(bigMonth, month) >= 0 && day == 31) {
return result = "昨天";
} else if (Arrays.binarySearch(smallMonth, month) >= 0 && day == 30) {
return result = "昨天";
} else if (month == 2) {
if (year % 4 == 0 || year % 400 == 0) {
if (day == 29) {
result = "昨天";
} else {
return DATE_ERROR;
}
} else {
if (day == 28) {
return result = "昨天";
} else {
return DATE_ERROR;
}
}
}
}
// 如果当前天数大于等于7天,则直接输出月日
if (currentDay >= 7) {
result = month + "月" + day + "日";
} else if (Arrays.binarySearch(bigMonth, month) >= 0 && 31 - day + currentDay < 7) {
// 把年月日转化为星期,大月
result = dayToWeek(year, month, day);
} else if (Arrays.binarySearch(smallMonth, month) >= 0 && 30 - day + currentDay < 7) {
// 把年月日转化为星期,小月
result = dayToWeek(year, month, day);
} else if (month == 2) {
if (currentYear % 400 == 0 || currentYear % 4 == 0) {
// 闰年
if (29 - day + currentDay < 7) {
result = dayToWeek(year, month, day);
} else {
result = month + "月" + day + "日";
}
} else {
// 平年
if (28 - day + currentDay < 7) {
result = dayToWeek(year, month, day);
} else {
result = month + "月" + day + "日";
}
}
} else {
result = month + "月" + day + "日";
}
return result;
}
/**
* 对下个月时间进行转换
*
* @param year 输入的年
* @param month 输入的月
* @param day 输入的日
* @param currentMonth 当前月
* @param currentDay 当前天
* @return 转换结果
*/
private static String dayToNextMonth(int year, int month, int day, int currentMonth, int currentDay) {
String result = null;
if (day == 1) {
if (Arrays.binarySearch(bigMonth, currentMonth) >= 0 && currentDay == 31) {
result = "明天";
} else if (Arrays.binarySearch(smallMonth, currentMonth) >= 0 && currentDay == 30) {
result = "明天";
} else if (currentMonth == 2) {
if (currentYear % 4 == 0 || currentYear % 400 == 0) {
if (currentDay == 29) {
result = "明天";
} else {
result = month + "月" + day + "日";
}
} else {
if (currentDay == 28) {
result = "明天";
} else {
result = month + "月" + day + "日";
}
}
} else {
result = month + "月" + day + "日";
}
} else {
result = month + "月" + day + "日";
}
return result;
}
/**
* 将日期转换为昨天,今天明天
*
* @param year 输入的年
* @param month 输入的月
* @param day 输入的天
* @param currentDay 当前天
* @return 转化结果
*/
private static String someday(int year, int month, int day, int currentDay) {
String result = null;
if (day == currentDay) {
result = "今天";
} else if (day - currentDay == 1) {
// 明天
result = "明天";
} else if (day - currentDay == -1) {
// 昨天
result = "昨天";
} else if (-7 <= day - currentDay && day - currentDay <= -2) {
// 上一周
result = dayToWeek(year, month, day);
} else {
// 不是同一天
result = month + "月" + day + "日";
}
return result;
}
/**
* 将年月日转换为星期
*
* @param year 输入的年
* @param month 输入的月
* @param day 输入的日
* @return 转换结果
*/
private static String dayToWeek(int year, int month, int day) {
String result = null;
if (DayOfWeek.MONDAY == LocalDate.of(year, month, day).getDayOfWeek()) {
result = "星期一";
} else if (DayOfWeek.TUESDAY == LocalDate.of(year, month, day).getDayOfWeek()) {
result = "星期二";
} else if (DayOfWeek.WEDNESDAY == LocalDate.of(year, month, day).getDayOfWeek()) {
result = "星期三";
} else if (DayOfWeek.THURSDAY == LocalDate.of(year, month, day).getDayOfWeek()) {
result = "星期四";
} else if (DayOfWeek.FRIDAY == LocalDate.of(year, month, day).getDayOfWeek()) {
result = "星期五";
} else if (DayOfWeek.SATURDAY == LocalDate.of(year, month, day).getDayOfWeek()) {
result = "星期六";
} else if (DayOfWeek.SUNDAY == LocalDate.of(year, month, day).getDayOfWeek()) {
result = "星期日";
}
return result;
}
/**
* 对日期的常规分割方法
*
* @param string 日期
* @return 分割结果
*/
private static String[] dateSplit(String string) {
// 日期时间的长度在8-10之间
if (string.length() < 8 || string.length() > 10) {
return null;
} else if (string.contains("-")) {
return string.split("-");
} else if (string.contains(".")) {
return string.split("\\.");
} else if (string.contains("/")) {
return string.split("/");
} else {
return null;
}
}
/**
* 处理时间
*
* @param string 时间
* @return 时间处理的结果
*/
private static String timeHandle(String string) {
String result = null;
String[] time = string.split(":");
if (time == null || time.length != 2) {
return TIME_ERROR;
} else if (!equalsNum(time[0]) || !equalsNum(time[1])) {
return TIME_ERROR;
} else {
String type = null;
String colon = null;
int h = Integer.valueOf(time[0]);
int m = Integer.valueOf(time[1]);
// 判断输入的小时和分钟的长度是否符合要求
boolean hLength = (time[0].length() > 0 && time[0].length() <= 2);
boolean mLength = (time[1].length() > 0 && time[1].length() <= 2);
// 不符合要求则提示错误
if (hLength == false || mLength == false) {
return TIME_ERROR;
} else if (h < 0 || h > 23 || m < 0 || m > 59) {
return TIME_ERROR;
} else {
type = h < 10 ? "0" : "";
colon = m < 10 ? ":0" : ":";
result = type + h + colon + m;
}
}
return result;
}
/**
* 判断输入的是不是数字
*
* @param string 输入的字符串
* @return 是否全是数字
*/
private static boolean equalsNum(String string) {
char[] c = string.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] > 57 || c[i] < 48) {
return false;
}
}
return true;
}
}
对输入的时间进行测试结果:
输入日期时间格式:xxxx-xx-xx xx:xx
例如: 2000-11-11 11:11
请输入: 2000-2-29 11:22
格式化结果:
2000年2月29日 11:22
2000-2-30 12:22
日期错误, 请重新输入
2000.2.22 12:22
格式化结果:
2000年2月22日 12:22
2010/12/22 12:22
格式化结果:
2010年12月22日 12:22
更多情况,请自行测试