这里要实现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. 识别输入错误的月份的英语单词

最新文章

  1. 基于spring注解AOP的异常处理
  2. noip模拟赛 纸壳子
  3. centos7 Nexus maven私有仓库
  4. C# WinForm 中Console 重定向输出到ListBox控件中显示
  5. hdu 1211 RSA
  6. 隐藏NavigationBar 带来的坑
  7. EffectiveC#02--仅在对基类进行强制更新时才使用new修饰符
  8. 如何查看npm配置?
  9. js+html+css简单的互动功能页面(2015知道几乎尖笔试题)http://v.youku.com/v_show/id_XMTI0ODQ5NTAyOA==.html?from=y1.7-1.2
  10. nginx四层负载均衡配置
  11. 任务调度---crontab
  12. $_FILES数组为空的原因
  13. 洛谷P2611 信息传递
  14. java二分法搜索
  15. Java8 方法引用
  16. 【转】通过 INotifyPropertyChanged 实现观察者模式
  17. 更新快排中的partition
  18. HDU 2064 汉诺塔III (递推)
  19. AmazeUI学习
  20. jq+download+文件夹下载

热门文章

  1. 2.MaxSubArray-Leetcode
  2. C++/Python冒泡排序与选择排序算法详解
  3. KEPServeEX 6与KepOPC中间件测试
  4. 1 — 第一个springboot
  5. 巩固java第四天
  6. mongDB进阶
  7. Linux—禁止用户SSH登录方法总结
  8. [转]C++中const的使用
  9. 连接 MySQL 数据库出现问题:The server time zone value ‘�й���׼ʱ��‘ is unrecogni....
  10. shell脚本统计多个CPU利用率