Integer i1 = new Integer(100);//把int类型的100转换成Integer
Integer i2 = new Integer("100");//把String类型的“100”转换成Integer
//Integer i2 = new Integer("abc");//把String类型的“abc”转换成Integer
//不能进行转换,数值类型格式不匹配,//NumberFormatException
Integer i1 = new Integer(100);//把int类型的100转换成Integer
Integer i2 = new Integer("100");//把String类型的“100”转换成Integer
//Integer i2 = new Integer("abc");//把String类型的“abc”转换成Integer
//不能进行转换,数值类型格式不匹配,//NumberFormatException
2.Integer封装类的valueof()
Integer i3 = Integer.valueOf(100);//把int类型的100转换成Integer
Integer i4 = Integer.valueOf("100");//把String类型的“100”转换成Integer
Integer i4 = Integer.valueOf("abc");//把String类型的“abc”转换成Integer
//不能进行转换,数值类型格式不匹配,//NumberFormatException
//String --- Integer ,Integer --- int
String s = "100"; Integer i1 = Integer.valueOf(s); int i2 = Integer.intValue(i1);
//String --- int
String s = "100"; int i = Integer.parseInt(s);
import java.util.Arrays;
//定义一个字符串 "27 98 49 16 57",打印输出 "16 27 49 57 98"
public class Test{
public static void main(String[] args){
//定义字符串
String s = "27 98 49 16 57";
//拆分成字符串数组
String[] strArr = s.split(" ");
//定义int数组
int[] arr = new int[strArr.length];
//把字符串数组中的每个元素,放到int数组中
for(int i = 0; i < arr.length; i++){
arr = Integer.parseInt(strArr);
}
//将int数组进行排序
Arrays.sort(arr);
//把int数组再转换成String类型,借用StringBuilder优化
StringBuilder sb = new StringBuilder();
for(int i=0; i< arr.length ; i++){
sb.append(arr);
if(i != arr.length - 1){
sb.append(" ");
}
}
String ss = sb.toString();
System.out.println(ss);
}
}
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = sdf.format(d);
system.out.println(s);//输出2019-04-28 11:11:11
String s = "2019-04-08 11:11:11";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdf.parse(s);
System.out.println(d);//Mon Apr 08 11:11:11 CST 2019
public class DateUtil {
private DateUtil(){};
//完成将Date对象格式化成字符串的功能
public static String dateToString(Date d, String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);
String s = sdf.format(d);
return s;
}
//将字符串日期解析成Date对象功能
public static Date StringToDate(String s,String format) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date d = sdf.parse(s);
return d;
}
}
public class DateUtilTest {
public static void main(String[] args) throws ParseException {
Date d = new Date();
String s = DateUtil.dateToString(d, "yyyy-MM-dd HH:mm:ss");
System.out.println(s);
String ss = "2019-04-28 20:49:26";
Date dd = DateUtil.StringToDate(ss, "yyyy-MM-dd HH:mm:ss");
System.out.println(d);
}
}
Calendar c = Calendar.getInstance();
c.get(Calendar.YEAR);//获取指定日历字段的当前系统时间值
c.add(Calendar.YEAR,5);//表示五年后的今天,将指定日历字段进行增加或减少
c.set(2018,01,01);//设置日历的年月日,输出2018-02-01,因为月是从0开始
public class CalendarTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个年份:");
int year = sc.nextInt();
Calendar c = Calendar.getInstance();
c.set(year,2,1);
c.add(Calendar.DATE,-1);
int date = c.get(Calendar.DATE);
System.out.println(year + "年二月有"+date+"天");
}
}
编译时异常 | 运行时异常 |
Exception类及其子类 | RuntimeException类及其子类 |
写代码时必须处理,否则无法运行 | 写代码时不需要处理 |
//异常类
public class ScoreException extends Exception {
public ScoreException(){}
public ScoreException(String message){
super(message);
}
}
//使用异常类
public class Teacher {
public void score(int score) throws ScoreException {
if (score<0 || score>100){
throw new ScoreException("你输入的分数不正确!");
}else {
System.out.println("分数正常");
}
}
}
//测试类
public class Test1 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("请输入一个分数:");
int score = sc.nextInt();
Teacher t = new Teacher();
try {
t.score(score);
} catch (ScoreException e) {
e.printStackTrace();
}
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |