Java - 尚学堂第八章常用类(将输入的string类型的值转为整数、浮点型、日期类型)

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; 
import java.util.GregorianCalendar;
import java.util.Scanner;

public class TestASE {
    public static void main(String[] args) throws ParseException {
        System.out.println("请输入年龄:");
        Scanner scanner = new Scanner(System.in);
        String age = scanner.nextLine();
        System.out.println("请输入分数:");
        String score = scanner.nextLine();
        System.out.println("请输入入学日期(格式为2020-10-20):");
        String date = scanner.nextLine();
        
        //转换为整数、浮点型、日期类型
        int age2 = Integer.parseInt(age);
        double score2 = Double.parseDouble(score);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date2 = df.parse(date);//先转为时间类型
        Calendar c = new GregorianCalendar();
        c.setTime(date2);
        
        System.out.println("年龄" + age2 + "岁\n"
                + "分数:" + score2 + "分\n"
                + "入学时间" + date2);
    }
}

转自https://blog.csdn.net/xxyh1234/article/details/89495876

posted on 2019-04-30 15:54  kl_1998  阅读(259)  评论(0编辑  收藏  举报

导航