两种格式日期java_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;

/**

*

* Title: Main

*

*

* Description:根据两种不同的输入日期格式进行判断并且进行转换

*

*

* @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" };

/**

*

* Title: main

*

*

* Description:main()方法,程序的入口

*

*

* @param args

*

*/

public static void main(String[] args) {

input();

output();

}

/**

*

* Title: input

*

*

* Description:输入需要计算的字符串

*

*

*/

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();

}

/**

*

* Title: judgeType

*

*

* Description:判断输入的日期格式类型,如果两张格式都不满足,则返回2,即输出错误

*

*

* @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;

}

/**

*

* Title: num2Words

*

*

* Description:1981.07.30 格式,输出 July 30.1981格式

*

*

* @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);

}

/**

*

* Title: word2Num

*

*

* Description:输入 July 30.1981格式 输出1981.07.30 格式

*

*

* @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);

}

/**

*

* Title: output

*

*

* Description:根据不同的日期格式进行输出

*

*

*/

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 格式

0818b9ca8b590ca3270a3433284dd417.png

2. 1981.07.30 格式转为July 30.1981格式

0818b9ca8b590ca3270a3433284dd417.png

3. 识别输入错误的日期格式

0818b9ca8b590ca3270a3433284dd417.png

4. 识别输入错误的月份的英语单词

0818b9ca8b590ca3270a3433284dd417.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值