java源码——两种格式日期的转换
这里要实现1981.07.30 格式和July 30.1981格式的日期的转换。
在输入时进行日期格式的识别,并且对字符串进行操作并且输出。
难点在于字符串格式的识别和月份的转换,我用了正则表达式匹配和字符串的裁剪转换为整形再在数组中取出对应月份做的。
字符串进行裁剪时要十分小心。同时,还要注意月份的转换的正确性。哈哈,以前很少使用正则表达式匹配呢。
不多说了,代码更多的是细心的书写,难度也不大。
上代码。
Main.java
package com.fuxuemingzhu.datetransfer.main; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * <p> * Title: Main * </p> * <p> * Description:根据两种不同的输入日期格式进行判断并且进行转换 * </p> * * @author fuxuemingzhu * * @email fuxuemingzhu@163.com * * @date 2014年11月24日 下午4:19:09 */ public class Main { /** * dateInput 输入的日期 */ public static String dateInput; /** * dateOutput 输出的日期 */ public static String dateOutput; /** * months 12个月份的英文名 */ public static String[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; /** * <p> * Title: main * </p> * <p> * Description:main()方法,程序的入口 * </p> * * @param args * */ public static void main(String[] args) { input(); output(); } /** * <p> * Title: input * </p> * <p> * Description:输入需要计算的字符串 * </p> * */ public static void input() { System.out.println("提示:日期格式有1981.07.30和July 30.1981两种。"); System.out .println("月的英文输入对照为:January,February,March,April,May,June,July,August,September,October,November,December."); System.out.println("请输入需要转换的日期:"); Scanner scanner = new Scanner(System.in); dateInput = scanner.nextLine(); scanner.close(); } /** * <p> * Title: judgeType * </p> * <p> * Description:判断输入的日期格式类型,如果两张格式都不满足,则返回2,即输出错误 * </p> * * @return 1981.07.30 格式,输出0 July 30.1981格式输出1 错误输出2 * */ public static int judgeType() { Pattern pattern1 = Pattern.compile("\\d{4,}\\.\\d{2,}\\.\\d{2,}"); Matcher matcher1 = pattern1.matcher(dateInput); boolean isMonth = false; int returnType = 2; if (matcher1.find()) { String month = dateInput.substring(5, 7); int monthInt = Integer.parseInt(month); if (monthInt <= 12 && monthInt >= 1) { returnType = 0; } } else { try { String month = dateInput.split(" ")[0].toString(); for (int i = 0; i < months.length; i++) { if (months[i].equals(month)) { isMonth = true; break; } } String yearAndMonth = dateInput.split(" ")[1].toString(); Pattern pattern2 = Pattern.compile("\\d{2,}\\.\\d{4,}"); Matcher matcher2 = pattern2.matcher(yearAndMonth); if (isMonth && matcher2.find()) { returnType = 1; } } catch (Exception e) { returnType = 2; } } return returnType; } /** * <p> * Title: num2Words * </p> * <p> * Description:1981.07.30 格式,输出 July 30.1981格式 * </p> * * @param numString * 1981.07.30 格式 * */ public static void num2Word(String numString) { String year = numString.substring(0, 4); String month = numString.substring(5, 7); String day = numString.substring(8); String monthWord = months[Integer.parseInt(month) - 1]; dateOutput = monthWord + " " + day + "." + year; System.out.println(dateOutput); } /** * <p> * Title: word2Num * </p> * <p> * Description:输入 July 30.1981格式 输出1981.07.30 格式 * </p> * * @param wordString * July 30.1981格式 * */ public static void word2Num(String wordString) { String year = wordString.split(" ")[1].substring(3); String monthWord = wordString.split(" ")[0].toString(); String day = wordString.split(" ")[1].substring(0, 2); int monthInt = 0; for (int i = 0; i < months.length; i++) { if (months[i].equals(monthWord)) { monthInt = i + 1; break; } } dateOutput = year + "." + monthInt + "." + day; System.out.println(dateOutput); } /** * <p> * Title: output * </p> * <p> * Description:根据不同的日期格式进行输出 * </p> * */ public static void output() { if (judgeType() == 0) { num2Word(dateInput); } else if (judgeType() == 1) { word2Num(dateInput); } else { System.err.println("输入日期有误!请按照提示的格式进行输入。"); } } }
正则匹配还是需要继续练一练的。
附上运行截图。
1. July 30.1981格式转为1981.07.30 格式
2. 1981.07.30 格式转为July 30.1981格式
3. 识别输入错误的日期格式
4. 识别输入错误的月份的英语单词