为了账号安全,请及时绑定邮箱和手机立即绑定
慕课网数字资源数据库体验端
Java入门第三季_学习笔记_慕课网
为了账号安全,请及时绑定邮箱和手机立即绑定

Java入门第三季

陈码农
难度入门
时长 5小时 0分
  • import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class HelloWorld { public static void main(String[] args) throws ParseException { // 使用format()方法将日期转换为指定格式的文本 SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH:mm"); SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 创建Date对象,表示当前时间 Date now = new Date(); // 调用format()方法,将日期转换为字符串并输出 System.out.println(sdf1.format(now)); System.out.println(sdf2.format(now)); System.out.println(sdf3.format(now)); // 使用parse()方法将文本转换为日期 String d = "2014-6-1 21:05:36"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 调用parse()方法,将字符串转换为日期 Date date = sdf.parse(d); System.out.println(date); } }
    查看全部
  • 代码中的 “yyyy-MM-dd HH:mm:ss” 为预定义字符串, yyyy 表示四位年, MM 表示两位月份, dd 表示两位日期, HH 表示小时(使用24小时制), mm 表示分钟, ss 表示秒,这样就指定了转换的目标格式,最后调用 format() 方法将时间转换为指定的格式的字符串。
    查看全部
  • 再来看,将字符串转换成基本类型有两种方法: 1. 调用包装类的 parseXxx 静态方法 2. 调用包装类的 valueOf() 方法转换为基本类型的包装类,会自动拆箱
    查看全部
  • 其中,基本类型转换为字符串有三种方法: 1. 使用包装类的 toString() 方法 2. 使用String类的 valueOf() 方法 3. 用一个空字符串加上基本类型,得到的就是基本类型数据对应的字符串
    查看全部
  • public class HelloWorld { public static void main(String[] args) { double m = 78.5; //将基本类型转换为字符串 String str1 = String.valueOf(m); System.out.println("m 转换为String型后与整数20的求和结果为: "+(str1+20)); String str = "180.20"; // 将字符串转换为基本类型 Double a = Double.valueOf(str) ; System.out.println("str 转换为double型后与整数20的求和结果为: "+(a+20)); } }
    查看全部
  • public class HelloWorld { public static void main(String[] args) { // 定义double类型变量 double a = 91.5; // 手动装箱 Double b = new Double(a); // 自动装箱 Double c = a; System.out.println("装箱后的结果为:" + b + "和" + c); // 定义一个Double包装类对象,值为8 Double d = new Double(87.0); // 手动拆箱 double e = d.doubleValue() ; // 自动拆箱 double f = d ; System.out.println("拆箱后的结果为:" + e + "和" + f); } }
    查看全部
  • public class HelloWorld { public static void main(String[] args) { // 定义int类型变量,值为86 int score1 = 86; // 创建Integer包装类对象,表示变量score1的值 Integer score2=new Integer(score1); // 将Integer包装类转换为double类型 double score3=score2.doubleValue(); // 将Integer包装类转换为float类型 float score4=score2.floatValue(); // 将Integer包装类转换为int类型 int score5 =score2.intValue(); System.out.println("Integer包装类:" + score2); System.out.println("double类型:" + score3); System.out.println("float类型:" + score4); System.out.println("int类型:" + score5); } }
    查看全部
  • public class HelloWorld { public static void main(String[] args) { // 创建一个空的StringBuilder对象 StringBuilder str = new StringBuilder(); // 追加字符串 str.append("jaewkjldfxmopzdm"); // 从后往前每隔三位插入逗号 for (int i=str.length()-3;i>0;i=i-3){ str.insert(i,","); } // 将StringBuilder对象转换为String对象并输出 System.out.print(str.toString()); } }
    查看全部
  • public class HelloWorld { public static void main(String[] args) { // 创建一个空的StringBuilder对象 StringBuilder str = new StringBuilder(); // 追加字符串 str.append("jaewkjldfxmopzdm"); // 从后往前每隔三位插入逗号 str.insert(str.length()-3,","); str.insert(str.length()-7,","); str.insert(str.length()-11,","); str.insert(str.length()-15,","); str.insert(str.length()-19,","); // 将StringBuilder对象转换为String对象并输出 System.out.print(str.toString()); } }
    查看全部
  • List接口及其实现类ArrayList
    查看全部
  • 一、java中的集合类:是一种工具 类,就像是容器,储存任意数量的具有共同属性的对象 二、集合的作用: (1)在类的内部,对数据进行组织 (2)简单而快速的搜索大数量的条目 (3)有集合接口,提供了一系列排列有序的元素,并且可以在序列中间快速的插入或者删除有关元素 三、集合组成: 根接口:collection map (主要) collection子接口: list:有序重复(序列) queue: 有序重复(队列) set:无序不重复(集) map子接口: HashMap 映射 key ,value键值对
    查看全部
  • public class HelloWorld { public static void main(String[] args) { // 定义一个字符串 String s = "aljlkdsflkjsadjfklhasdkjlflkajdflwoiudsafhaasdasd"; // 出现次数 int num = 0; // 循环遍历每个字符,判断是否是字符 a ,如果是,累加次数 for ( int i=0; i<s.length();i++ ) { // 获取每个字符,判断是否是字符a if ( s.charAt(i) == 'a' ) { // 累加统计次数 num++; } } System.out.println("字符a出现的次数:" + num); } }
    查看全部
  • 我写的答案
    查看全部
  • public class HelloWorld { public static void main(String[] args) { String s1 = "imooc"; String s2 = "imooc"; //定义字符串s3,保存“I love”和s1拼接后的内容 String s3 ="I love"+ s1; // 比较字符串s1和s2 // imooc为常量字符串,多次出现时会被编译器优化,只创建一个对象 System.out.println("s1和s2内存地址相同吗?" + (s1 == s2)); //比较字符串s1和s3 System.out.println("s1和s3内存地址相同吗?" + (s1 == s3) ); String s4 = "I love " + s1; //比较字符串s4和s3 // s1是变量,s4在运行时才知道具体值,所以s3和s4是不同的对象 System.out.println("s3和s4内存地址相同吗?" + (s4 == s3)); } }
    查看全部
  • 字符串类型String: 1. 字符串 str 中字符的索引从0开始,范围为 0 到 str.length()-1 2. 使用 indexOf 进行字符或字符串查找时,如果匹配返回位置索引;如果没有匹配结果,返回 -1 3. 使用 substring(beginIndex , endIndex) 进行字符串截取时,包括 beginIndex 位置的字符,不包括 endIndex 位置的字符
    查看全部

举报

0/150
提交
取消
课程须知
此部分为 Java 课程的进阶内容,适合具有一定 Java 基础的伙伴们学习,如果您是新手,建议您移步 《Java入门第一季》 和 《Java入门第二季》,在理解并掌握面向对象相关知识后再回来进修。
老师告诉你能学到什么?
本课程将学习 Java 中的异常处理、集合框架、字符串、常用类等,逐步学习掌握 Java 高级技术。
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!