第十二次课
一.
package com.hpe.demo;
/**
- object�����ʾ������
- @author Administrator
- @see
*/
public class Demo1 {
public String name;
@Override
public boolean equals(Object obj) {
if(obj == null) {
return false;
}
if(!(obj instanceof Demo1)) {
return false;
}
Demo1 demo1 = (Demo1)obj;
if(demo1.name.equals("hpe")) {
return true;
}
return true;
}
@Override
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
}
二.
package com.hpe.demo;
/**
- object����equals���������
- @author Administrator
*/
public class Demo2 {
private int id;
private String name;
@Override
public boolean equals(Object obj) {
// �ǿ�
if(obj == null) {
return false;
}
if(!(obj instanceof Demo2)) {
return false;
}
Demo2 demo2 = (Demo2)obj;
if(demo2.getId() == this.getId() && demo2.getName().equals(this.getName())) {
return true;
}
return false;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
// object �еıȽ�
Demo2 demo1= new Demo2(); // ʵ��1
demo1.setId(1);
demo1.setName("zhang");
Demo2 demo2= new Demo2(); // ʵ��2
demo2.setId(1);
demo2.setName("zhang");
System.out.println(demo1 == demo2); // false
System.out.println(demo1.equals(demo2)); // false
}
}
三 .
package com.hpe.demo;
/**
- string�����ʾ��
- @author Administrator
*/
public class Demo3 {
@Override
public String toString() {
return "demo3:" + "id " + "name";
}
public static void main(String[] args) {
String str1 = "abcdef";
String str2 = "abcdeg";
System.out.println(str1.equals(str2));
System.out.println(str1 == str2);
Demo1 a = new Demo1();
Demo2 b = new Demo2();
Demo3 c = new Demo3();
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
四.
package com.hpe.demo;
public class Demo4 {
public static void main(String[] args) {
// һ���ַ���������������ֳ�������
String str1 = “����ʵѵ����”;
String str2 = "����ʵѵ����";
String str3 = new String("����ʵѵ����");
System.out.println(str2 == str3);
System.out.println(str1 == str2); //
System.out.println(str1.equals(str2));
//1���ַ����ıȽϣ�== equals����
// new String(""); = "";
/*
* String str1 = "abc"; ���Ȼ��ڳ������в���abc,����ҵ�abc���Ͱ�abc�ַ������õ�ַ����str1.
* ���û���ҵ����Ͱ�abc�ŵ���������Ȼ�����ڴ��п���һ��str1�Ŀռ䣬������õ�ַ��
*
* new String("abc"); ����һ��str1�Ŀռ䣬Ȼ���ٲ��ҡ�
*
* �ܽ�ַ���ʹ�ý���ѡ�� String str = ������
*/
String s = "abc";
// �ֽ�����
char[] charArray = s.toCharArray();
// 1\�滻�ַ��� replace(); ������ʽ
String s1 = s.replace("b", "123");
System.out.println(s);
System.out.println(s1);
//2\�ж��ַ����Ƿ����ָ���ַ�����contains()
String string1 = "abcef";
boolean flag = string1.contains("a");
boolean flag1 = string1.contains("a1");
System.out.println(flag + " " + flag1);
// 3\�Ƚ��ַ��Ĵ�С�� ASCII���ֵ�
String string2 = "abc";
String string3 = "ac";
int c1 = string2.compareTo(string3);
System.out.println(c1);
// 4\ indexof lastindexof �ַ�����һ�λ����һ�γ��ֵ�λ�á��±�
String str4 = "�л�����,����,��ͬ��";
int f1 = str4.indexOf(',');
int l1 = str4.lastIndexOf(',');
System.out.println(f1 + " " + l1);
// 5\�ƶ�����λ�ã���ȡ�ַ�����
String newStr1 = str4.substring(4);
System.out.println(str4.substring(str4.indexOf(',') + 1,str4.lastIndexOf(',')));
System.out.println(str4.substring(0,str4.indexOf(',')));
// 6\ length();
// 7\�����ƶ��ַ���ת�������顣split(); ������ʽ
// ��Ƶġ������ָ�. | ^ - ||
String str5 = "�Ǻ�-���-�ټ�-����,����ȥ����";
String[] array = str5.split("-");
System.out.println(array.length);
for (int i = 0; i < array.length; i++) {
String temp = array[i];
System.out.println(temp);
}
// 8\ȥ���ո� �� ��� �� ���� �� ==> "��� �� ����"
String str6 = " ��� �� ���� ";
String str7 = str6.trim();
System.out.println(str7);
// 9\charAt
char c = str4.charAt(1);
System.out.println(c);
// 10\concat�ַ��� ƴ�� "�Ұ�" + "ѧϰ"��===�� "�Ұ�ѧϰ"
String str8 = "�Ұ�".concat("ѧϰ");// sql
System.out.println(str8);
}
/**
* ͳ��nba���ִ���
*/
public static int getCount(String str,String flag) {
int count = 0;
int index = 0;
while(str.indexOf(flag,index) > 0) {
index = str.indexOf(flag,index) + 1;
count ++;
}
return count;
}
/*
*
*/
}
五.
package com.hpe.demo;
/**
- ���Է�����== �Ƚϵ�ʹ��
- @author Administrator
*/
public class Test {
public static void main(String[] args) {
Demo1 demo1 = null;
Demo1 demo2 = null;
Demo1 demo3 = null;
demo1 = new Demo1(); // ʵ��1 ��
demo2 = demo1;
demo3 = demo1;
System.out.println(demo1 + " " + demo2 + " " + demo3 );
System.out.println(demo1 == demo2);
System.out.println(demo3 == demo2); // �Ƚϵĵ�ַ��
Demo1 demo4 = new Demo1(); // ʵ��2
System.out.println(demo4);
demo4 = demo3;
System.out.println(demo1 == demo4);
// com.zhrm.jnsx
}
}
另一个文件
一.
package com.hpe.demo;
import java.util.Iterator;
/**
- stringbuffer类演示
- @author Administrator
*/
public class Demo1 {
public static void main(String[] args) {
//
StringBuffer sb = new StringBuffer();
// select * from tb_user where id = ‘123’ and name = ‘zhang’
String id = args[0];
String name = args[1];
sb.append(" select * from tb_user “);
sb.append(” where id = ‘");
sb.append(id);
sb.append("’ and name = ‘");
sb.append(name);
sb.append("’ ");
System.out.println(sb);
// 拼接json数据 【{“id”:“123”,“name”:“zhangsan”},{“id”:“123”,“name”:“zhangsan”}】
Person[] ps = new Person[3];
Person p1 = new Person();
p1.id = 1;
p1.name = "zhangs1";
Person p2 = new Person();
p2.id = 2;
p2.name = "zhangs2";
Person p3 = new Person();
p3.id = 3;
p3.name = "zhangs3";
ps[0] = p1;
ps[1] = p2;
ps[2] = p3;
StringBuffer sbf = new StringBuffer();
sbf.append("[");
for (int i = 0; i < ps.length; i++) {
Person p = ps[i];
//【{“id”:123,“name”:“zhangsan”},{“id”:.。。。。。】
sbf.append("{\"id\":");
sbf.append(p.id);
sbf.append(",\"name\":\"");
sbf.append(p.name+"\"},"); //
}
// [{"id":1,"name":"zhangs1"},{"id":2,"name":"zhangs2"},{"id":3,"name":"zhangs3"},
sbf.deleteCharAt(sbf.length() - 1);
sbf.append("]");
System.out.println(sbf.toString());
int a = 1;
}
}
class Person{
public int id;
public String name;
}
二.
package com.hpe.demo;
import java.util.*;
/**
- 1、包装类: a、面向对象编程思想;b,基本数据类型变量,不能用对象使用的问题。 c、传输过程中对象传输,容器中皆为对象。
- int --》 Integer
- 2、装箱和拆箱的过程 (手动,自动)
- 基本数据 —》 对应的包装类 : 装箱
- 拆箱: 包装类—》对应的基本数据类型
- 3、 基本数据类型和String类型的转换
- 一、基本数据类型转化为String类型
- a, + “”;
- b, String.valueof(基本数据类型);
- 二、String转化基本数据类型(包装类)
-
包装类.parseXXXX(字符串);
- @author Administrator
*/
public class Demo2 {
public static void main(String[] args) {
List list = new ArrayList();
list.add(1);
int[] ii = {1,3,4};
System.out.println(list);
Integer i = new Integer(100);
int sum1 = 12 + i.intValue(); // 手动的拆箱
int sum = 12 + i; // 自动拆箱
System.out.println(12 + "12"); // 1212
String a = 12 + "";
int b = 100;
String.valueOf(b);
Integer bb = new Integer(b);
String bbStr = bb.toString();
String abc = "3.1415";
Double s = Double.parseDouble(abc);
System.out.println(12 + "");
}
}
三.
package com.hpe.demo;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
- java.util.Date 日期类
- @author Administrator
*/
public class Demo3 {
public static void main(String[] args) {
// 补充 java.sql.Date 只能获取年月日
java.sql.Date sqlDate = new java.sql.Date(1597371055149L);
//
System.out.println(sqlDate);
Date d2 = new Date(1597371055149L);
System.out.println(d2);
Date oldDate = getDate("2019 08 14 10:19:40", "yyyy MM dd HH:mm:ss");
while(true) {
Date d1 = new Date(); // 2020-08-14 10:08:48.129 yyyy MM dd HH:mm:ss
System.out.println(d1);
/**
* 1\ yyyy-MM-dd HH:mm:ss
* 2\ yyyy/MM/dd
* 3\ MM/yy
* 4\ HH:mm:ss
*/
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
String dateStr = sdf.format(d1);
System.out.println(dateStr);
System.out.println(d1.getTime());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 字符串转化为Date类 “2020 08 14 10:19:40”
* @param strDate
* @return
*/
public static Date getDate(String strDate,String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date d = null;
try {
d = sdf.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}
}
四.
package com.hpe.demo;
import java.util.Calendar;
/**
- Calendar日历类使用
- @author Administrator
*/
public class Demo4 {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
c.set(2020, 8, 14);
// 获取年
int year = c.get(c.YEAR);
// 获取月
int month = c.get(c.MONTH);
// 获取日
c.get(c.DAY_OF_MONTH);
c.get(c.HOUR_OF_DAY);
c.get(c.MINUTE);
c.get(c.SECOND);
System.out.println(year + " " + month );
}
}
五.
package com.hpe.demo;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
- java8 新特性 日期
- 旧的: Date非线程安全的。API设计的比较乱。
- java8:
- java.time包下:
- LocalDate : 年月日
- LocaLDateTime: 年月日时间
- LocatTime: 时间
- @author Administrator
*/
public class Date5 {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
System.out.println(“当前日期:” + date);
// 获取 年月日,周几
System.out.println(“年” + date.getYear());
System.out.println(“月” + date.getMonth());
System.out.println(“月(数字)” + date.getMonthValue());
System.out.println(“日” + date.getDayOfMonth());
System.out.println("周 " + date.getDayOfWeek());
// 去年 明年
LocalDate date1 = date.plusYears(1);
System.out.println("加1 " + date1.getYear());
LocalDate date2 = date.plusYears(-1);
System.out.println("减一 " + date2.getYear());
LocalDate date3 = date.plusMonths(1);
System.out.println("加 月份:" + date3.getMonthValue());
// 日期比较
boolean ifafter = date1.isAfter(date);
System.out.println(ifafter);
Boolean ifbefore = date1.isBefore(date);
System.out.println(ifbefore);
// LocaLDateTime
LocalDateTime dateTime = LocalDateTime.now();
System.out.println(dateTime);
// DateTimeFormatter LocalDateTime类转化字符串
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String dateTimeStr = dtf.format(dateTime);
System.out.println(dateTimeStr);
// 字符串转化成LocalDateTime LocalDate LocalTime
LocalDateTime.parse(dateTimeStr,dtf);
LocalDate.parse(dateTimeStr,dtf);
LocalTime.parse(dateTimeStr,dtf);
// 日期时间差计算类 java.time.Duration
LocalDateTime today = LocalDateTime.now();
System.out.println("today :" + today);
// 2020-12-12 12:12:12
LocalDateTime datenew = LocalDateTime.of(2020, 12, 12, 12,12,12);
// 日期差
Duration dur = Duration.between(today, datenew);
// 相差的天数
System.out.println(dur.toDays());
System.out.println(dur.toHours());
System.out.println(dur.toMillis());
}
public void testLocalDateTime() {
}
}