Java学习代码6

DateDemo

package com.mashibing;

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

/**
 * @author: 马士兵教育
 * @create: 2019-09-07 20:14
 */
public class DateDemo {
    public static void main(String[] args) throws ParseException {
        Date date = new Date();
        System.out.println(date);
        System.out.println(date.getTime());
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //将Date类按照规范转换为字符串格式
        String str = dateFormat.format(date);
        System.out.println(str);
        //将字符串转换成对应的日期类
        Date d1 = dateFormat.parse("2010-10-10 20:20:20");
        System.out.println(d1);

        //获取的是当前系统的时间
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar);
        //设置指定时间的日历类
        calendar.setTime(d1);
        System.out.println(calendar);
        System.out.println(calendar.get(Calendar.YEAR));
        System.out.println(calendar.get(Calendar.MONTH));
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
        System.out.println(calendar.get(Calendar.MINUTE));
        System.out.println(calendar.get(Calendar.SECOND));

    }
}

EventEnum

package com.mashibing;

public enum EventEnum {

    LAUNCH("launch"),PAGEVIEW("pageview"),EVENT("event");

    EventEnum(String name){
        this.name = name;
    }
    private String name;

    public void show(){
        System.out.println(this.name);
        EventEnum[] ee = values();
        for(int i = 0;i<ee.length;i++){
            System.out.println(ee[i]);
        }
    }

}

Gender

package com.mashibing;

public enum Gender {,}

IntegerDemo

package com.mashibing;

/**
 * @author: 马士兵教育
 * @create: 2019-09-07 16:09
 */
/*
* 包装类与基本数据类型
*   包装类是将基本数据类型封装成一个类,包含属性和方法
*   使用:
*       在使用过程中,会涉及到自动装箱和自动拆箱
*       装箱:将基本数据类型转换成包装类
*       拆箱:将包装类转换成基本数据类型
*
*
*
*
* */
public class IntegerDemo {
    public static void main(String[] args) {
//        int a = 10;
//        Integer i = new Integer(10);
//        //通过方法进行类型的转换
//        Integer i2 = Integer.valueOf(a);
//        int i3 = i.intValue();
//        System.out.println(a == i);
//        Float f1 = new Float(3.14);
//        Double d1 = new Double(3.14);
//        int i =100;
//        Integer i1 = 100;
//        Integer i2 = 100;
//        Integer i3 = 200;
//        Integer i4 = 200;
//        System.out.println(i1==i2);
//        System.out.println(i3==i4);

//        Double d1 = 1.0;
//        Double d2 = 1.0;
//        Double d3 = 2.0;
//        Double d4 = 2.0;
//        System.out.println(d1==d2);
//        System.out.println(d3==d4);

        Integer i = 10;
        int a = i;
        System.out.println(a==i);
    }
}

MathDemo

package com.mashibing;

/**
 * @author: 马士兵教育
 * @create: 2019-09-07 20:42
 */
public class MathDemo {
    public static void main(String[] args) {
        System.out.println(Math.abs(-1));
        System.out.println(Math.sqrt(2));
        System.out.println(Math.ceil(-3.14));
        System.out.println(Math.floor(-3.14));
        System.out.println(Math.pow(2,3));

    }
}

StringBufferDemo

package com.mashibing;

/**
 * @author: 马士兵教育
 * @create: 2019-09-07 20:04
 */
/*
* 可变字符串
*   StringBuffer:线程安全,效率低
*   StringBuilder: 线程不安全,效率高
*
*
* */
public class StringBufferDemo {
    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(1).append(1.234).append("abc").append(true);
        System.out.println(stringBuffer);
        System.out.println(stringBuffer.length());
        System.out.println(stringBuffer.capacity());
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("123").append(1).append(false);
        System.out.println(stringBuilder);
    }
}

StringDemo

package com.mashibing;

/**
 * @author: 马士兵教育
 * @create: 2019-09-07 16:28
 */
/*
* 注意:常量池在1.7之后放置在了堆空间之中
*       字符串的使用:
*           1、创建
*               String str = "abc";
*               String str2 = new String("abc");
*               两种方式都可以用,只不过第一种使用比较多
*           2、字符串的本质
*               字符串的本质是字符数组或者叫做字符序列
*               String类使用final修饰,不可以被继承
*               使用equals方法比较的是字符数组的每一个位置的值
*               String是一个不可变对象
*
* */
public class StringDemo {
    public static void main(String[] args) {
        String str = "abc";
        String str2 = new String("abc");
//        str2 = str2.intern();
        System.out.println(str==str2);
        System.out.println(str.equals(str2));
        System.out.println(str.charAt(0));
        //数组的复制过程
        System.out.println(str.concat("cde"));
        //返回指定下标的元素
        System.out.println(str.indexOf("a"));
        String s = "abcdefghijklmn";
        System.out.println(s.substring(3));
        //在截取字符串的时候,需要注意是左闭右开区间
        System.out.println(s.substring(3,5));
        System.out.println(s.length());
        System.out.println("-----------------");
//        String a = "abc";
//        String b = new String("abc");
//        b = b.intern();
//        System.out.println(a==b);

        String a = "abc";
        String b = "def";
        String c = "abcdef";
        String d = (a+b).intern();
        String e = "abc"+"def";
        System.out.println(c==d);
        System.out.println(c==e);
        String f = "a" + "b" +"c";
        String a1 = "a";
        String a2 = "b";
        String a3 = "c";
        String f1 = a1+a2+a3;


    }
}

Test

package com.mashibing;

/**
 * @author: 马士兵教育
 * @create: 2019-09-07 20:47
 */
public class Test {
    Gender gender = Gender.;
    Gender gender2 = Gender.;

    public static void main(String[] args) {
        EventEnum ee = EventEnum.LAUNCH;
        ee.show();
        String name = EventEnum.PAGEVIEW.name();
        System.out.println(name);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值