JAVA制作邮箱格式和时间格式
邮箱格式要求:
package com.email;
import java.util.Scanner;
public class EmailTest {
static Scanner input = new Scanner(System.in);
/**
* @param args
*/
public static void main(String[] args) {
boolean isgo = false;
do{
System.out.println("请输入您的邮箱号码:"); String str = input.next();
if(Email(str)){
System.out.println("邮箱格式正确"); isgo = false;
}
else{
System.out.println("邮箱格式不正确");
System.out.println("是否继续:1——继续;
int index = input.nextInt();
if(index == 1)
isgo = true;
else{
System.out.println("系统已退出"); isgo = false;
}
}
}while(isgo);
}
public static boolean Email(String str)
{
boolean isok = false;
if(str != null)
{
int num = 0; 任意数字键——退出");
for(int i = 0;i < str.length();i++) num++; if(num > 8) { ‘A’))
String c1 = str.substring(num-4, num-3); String c2 = str.substring(num-8, num-7); if(c1.equals(".")&&c2.equals("@")) { String temp1 = str.substring(num-3, num); if(temp1.equals("com")
emp1.equals("net")) { String temp2 = str.substring(num-7, um-4); char[] ch = [2] <= ‘9’&&ch[2] >= ‘1’) isok = n
true; else isok = false; }else isok = false; }else { System.out.println("邮箱号码格式3不符合要求"); isok =
false; } }else { System.out.println("邮箱号码格式2不符合要求
"); isok = false;
}
} } } else{ System.out.println("邮箱号码格式1不符
合要求"); } isok = false; }else{ System.out.println("邮箱号码长度不
足"); isok = false; } }else isok = false; return isok;
Time包: package com.time;
import java.util.Scanner;
public class Time {
public void timeshow() { Scanner input = new
Scanner(System.in); System.out.println("请输入年月日(XXXX年XX月
XX日XX点XX分XX秒)"); String time1 = input.next(); if(time1.length() == 20) { boolean isyear = false; boolean ismonth = false; boolean isday = false; boolean ishour = false; boolean isminute = false; boolean issecond =false; String t1 = time1.substring(4,5); String t2 = time1.substring(7,8);
String t3 = time1.substring(10,11);
String t4 = time1.substring(13, 14);
String t5 = time1.substring(16, 17);
String t6 = time1.substring(19, 20);
if(t1.equals("年")&&t2.equals("月
")&&t3.equals("日")&&t4.equals("点
")&&t5.equals("分")&&t6.equals("秒
"))
{ String year = time1.substring(0, 4); String month = time1.substring(5, 7); String day = time1.substring(8, 10); String hour = time1.substring(11, 13); String minute = time1.substring(14, 16); String second = time1.substring(17, 19); for(int i = 0; i < year.length();i++) { }
if(isyear) { for(int i = 0;i < month.length(); i++) { }
if(ismonth) { if (Integer.parseInt(month) >= 1 int num = Character.getNumericValue(month.charAt(i)); if (num >= 0 && num <= 9) ismonth = true; else ismonth = false; if(!ismonth) break; int num =
Character.getNumericValue(year.charAt(i)); if (num >= 0 && num <=
// 如果不符合就跳出,不9) else isyear = false; if(!isyear) break; 在判断 isyear = true;
31)
{
&& Integer.parseInt(month) <= 12)
ismonth = true;
for (int i = 0; i < day.length(); i++) { }
if (isday) { if (Integer.parseInt(day) >= 1&& Integer.parseInt(day)
<=
{
isday = true;
for(int i = 0; i < hour.length(); i++) {
int
num
=
int num = Character.getNumericValue(day.charAt(i)); if (num >= 0 && num <= 9) isday = true; else
isday = false;
if (!isday) // 如果不符合就跳出,不在判断 break;
Character.getNumericValue(hour.charAt(i));
}
if(num >= 0&&num <= 9) ishour = true; else
ishour = false;
if(!ishour) // 如果不符合就跳出,不在判
break;
断
if(ishour) {
if(Integer.parseInt(hour) {
ishour = true;
for(int i = 0; i < minute.length(); i++) {
>=
0&&Integer.parseInt(hour) <= 59)
}
int num =
Character.getNumericValue(minute.charAt(i));
if(num >= 0&&num <= 9) isminute = true; else
isminute = false;
if(!isminute) // 如果不符合就跳
break;
出,不在判断
if(isminute) {
if(Integer.parseInt(minute) {
>=
0&&Integer.parseInt(minute) <= 59)
isminute = true;
for(int i = 0; i < second.length(); {
int
num
=
i++)
Character.getNumericValue(second.charAt(i));
符合就跳出,不在判断
if(num >= 0&&num <= 9) issecond = true; else issecond = false;
if(!issecond) // 如果不
break; }
if(issecond) {
if(Integer.parseInt(second) >= 0&&Integer.parseInt(second) <= 59)
issecond = true;
}
ishour = false;
}
else issecond = false;
}else
isminute = false;
}else
} }else
//
} } } } isday = false; } } else ismonth = false; if(isyear&&ismonth&&isday) System.out.println("输入的时间正确"); else System.out.println("输入的时间格式不正确
"); }else{ System.out.println("时间字段不足"); } String str =
"1970年01月01号00点00分00秒"; int str1 =
Integer.parseInt(time1.substring(0, 4)) - 1970; int str2 = Integer.parseInt(time1.substring(5, 7)) - 1; int str3 = Integer.parseInt(time1.substring(8, 10)) - 1; int str4 = Integer.parseInt(time1.substring(11, 13)); int str5 =
Integer.parseInt(time1.substring(14, 16)); int str6 =
Integer.parseInt(time1.substring(17, 19)); System.out.println(); System.out.println("您输入的时间距离1970年1月1日00点00分00秒:
"); System.out.println(str1 + "年" + str2 + "个月" + str3
+ "天" + str4 + "小时" + str5 + "分 int time = str4 +
str3 * 24 + str2 * 24 * 30 + str1 * 24 * 30 * 12; System.out.println(); System.out.print("或者说,距离1970年1月1日000点00分00秒");
System.out.println(time +"小时" + str5 + "分" + str6 + "
秒"); " + str6 + "秒");
}
Time的测试包: package com.time;
public class timeTest {
/** * @param args
} */ public static void main(String[] args) { Time t = new Time(); } t.timeshow();