Java-日期类,正则实验

1. 随机产生两个日期时间,输入按时间先后顺序输出

public class RandomDate {

	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date1 = randomDate("2001-08-30 10:00:00");
		Date date2 = randomDate("2001-08-30 09:00:00");
		String formatDate1 = sdf.format(date1);
		String formatDate2 = sdf.format(date2);
		// 直接比较时间戳即可
		if (date1.getTime() > date2.getTime()) {
			System.out.println(formatDate2);
			System.out.println(formatDate1);
		} else {
			System.out.println(formatDate1);
			System.out.println(formatDate2);
		}
	}

	/**
	 * 
	 * @Title: randomDate
	 * @Description: 根据指定字符串创建随机时间
	 * @param: @param  str
	 * @param: @return Date
	 * @param: @throws ParseException
	 * @author: KangXiaoZhuang
	 * @email: ITkxz0830@163.com
	 */
	public static Date randomDate(String str) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long endTime = System.currentTimeMillis();
		// 指定时间 先转换成Date再转换为时间戳
		long startTime = sdf.parse(str).getTime();
		// 获取一个随机数
		long random = (long) (Math.random() * (endTime - startTime) + startTime);
		// 根据随机数来创建一个新的日期
		Date randomDate = new Date(random);
		// 返回randomDate
		return randomDate;
	}
}

在这里插入图片描述

2.定义一个方法自己实现toBinaryString方法的效果,将一个十进制整数转换成字符串表示的二进制

public class MytoBinaryString {

	public static void main(String[] args) {
		System.out.println("输入一个数,转为二进制字符串!");
		Scanner scanner = new Scanner(System.in);

		String binaryString = toBinaryString(scanner.nextInt());

		System.out.println(binaryString);
	}

	/**
	 * 
	 * @Title: toBinaryString
	 * @Description: 数字转为二进制
	 * @param: @param n
	 * @author: KangXiaoZhuang
	 * @email: ITkxz0830@163.com
	 */
	public static String toBinaryString(int n) {
		int mod = 0;
		// 创建栈来存储余数
		Deque<Integer> stack = new ArrayDeque<>();
		do {
			// 获取余数
			mod = n % 2;
			// stack.push(mod);
			stack.addFirst(mod);
			// 获取商
			n = n / 2;
		} while (n > 0);
		return stack.toString();
	}
}

在这里插入图片描述

3.请使用代码实现计算你活了多少天的方法,要求方法的输入参数为年月日的字符串,如“1988年12月13日”

public class CountDay {

	public static void main(String[] args) throws ParseException {

		// 1.时用Scanner类中的方法next,获取出生日期
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入你的出生日期,格式:yyyy-MM-dd");
		count(scanner.next());
	}

	/**
	 * 
	 * @Title: count
	 * @Description: 计算天数
	 * @param: @param  str
	 * @param: @throws ParseException
	 * @author: KangXiaoZhuang
	 * @email: ITkxz0830@163.com
	 */
	public static void count(String str) throws ParseException {

		// 2.时用DateFormat类中的方法parse,把字符串得出生日期,解析为Date格式的出生日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		// 3.把Date格式的出生日期转换为毫秒值
		Date birthdayDate = sdf.parse(str);

		long birthdayDateTime = birthdayDate.getTime();

		// 4.获取当前的日期,转换为毫秒值
		long todayTime = new Date().getTime();

		// 5.使用当前日期的毫秒值 - 出生日期的毫秒值
		long time = todayTime - birthdayDateTime;

		// 6.把毫秒值转换为天(s/1000/60/60/24)
		long day = time / 1000 / 60 / 60 / 24;
		System.out.println("你已经出生了" + day + "天");
	}
}

在这里插入图片描述

4.假设你的出生年月日为:2000-11-11,请用字符串表示这个数据,并将其转换为:2000年11月11日

public class StirngToDate {

	public static void main(String[] args) throws ParseException {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你的出生年月:");
		String birthDate = sc.next();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d = sdf.parse(birthDate);
		System.out.println("你的出生年月为:" + new SimpleDateFormat("yyyy年MM月dd日").format(d));
		sc.close();
	}
}

在这里插入图片描述

5.自己实现parseInt方法的效果,将字符串形式的数据装换成整数

要求:(1)最少一位,最多10位;
(2)0不能开头;
(3)字符串中只能是数字,不能有其他字符,否则显示数据格式有误

public class StringToInt {

	public static void main(String[] args) {
		boolean flag = true;
		while (flag) {
			System.out.println("请输入字符串:\n");
			Scanner sc = new Scanner(System.in);
			String str = sc.nextLine();
			if (chcek(str)) {
				int val = Integer.parseInt(str);
				System.out.println("转换后的整数:" + val);
				flag = false;
				break;
			}
		}
	}

	/**
	 * 
	 * @Title: chcek
	 * @Description: 校验输入的字符串是否匹配要求
	 * @param: @param  str
	 * @param: @return
	 * @author: KangXiaoZhuang
	 * @email: ITkxz0830@163.com
	 */
	public static boolean chcek(String str) {
		if (str.matches("^0\\d*$")) {
			System.err.println("不能以0开头");
			return false;
		} else if (!str.matches("\\d*")) {
			System.err.println("数据格式有误!只能输入数字");
			return false;
		} else if (!str.matches("^\\d{1,10}$")) {
			System.err.println("最少1位,最多10位");
			return false;
		}
		return true;
	}
}

在这里插入图片描述

6. 请把下面文本中的电话、邮箱、手机号、热线电话都爬取出来

“欢迎来到南京XX学院学习,
电话:18512516785,18512508079,
或者联系邮箱:boniu@cucn.cn,
座机电话:025-36517899,025-98951256,
邮箱:bozai@cucn.cn,
热线电话:400-618-8080,400-618-6000,400-618-4000”

public class RegexData {
	public static void main(String[] args) {
		String s1 = "欢迎来到南京XX学院学习,电话:18512516785,18512508079" + "或者联系邮箱:baaaiu@cucu.cn"
				+ "座机电话:025-36517899,025-98951256," + "邮箱:bozai@cucu.cn"
				+ "热线电话:400-618-8080,400-618-6000,400-618-4000";
		String regex = "(0\\d{2,6}-?\\d{5,20})|(1[3-9]\\d{9})|"
				+ "(\\w{1,30}@[0-9a-zA-z]{2,20}(\\.[0-9a-zA-Z]{2,20}){1,2})|" + "(400-?\\d{3,20}-?\\d{3,20})";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(s1);
		while (matcher.find()) {
			System.out.println(matcher.group());
		}
	}
}

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值