1.StringUtils.isEmpty、StringUtils.isBlank
两个方法都是判断字符是否为空的。前者是要求没有任何字符,即str==null 或 str.length()==0;后者要求是空白字符,即无意义字符
2.替换指定位置的指定字符串
public static void main(String[] args) {
StringBuffer buffer = new StringBuffer("123456");
System.out.println(buffer.toString());//输出123456
buffer.replace(0, 1, "a");
System.out.println(buffer.toString());//输出a23456
}
replace方法一共有三个参数,第一个参数是指定要替换的字符串的开始位置,第二个参数是指定要替换的字符串的结束位置(注意这里的结束位置不包括本身),第三个参数是指定想将字符串替换成什么内容
3.指定位置添加字符串
public static void main (String [] args){
String str="20190605";
StringBulider sb=new StringBuilder(str);
sb.insert(6,"-");
sb.insert(4,"-");
System.out.println(sb.toString());
}
4.集合和字符串相互转换
4.1:集合转字符串
List<String> idList= Lists.newArrayList();
idList.add("1");idList.add("2");idList.add("3");
String ids = StringUtils.join(idList,",");
4.2:字符串转集合
String str="1,2,3";
String[] strs=str.split(",");
List list=Arrays.asList(strs);
5.取出Map集合key-value
Map<Integer, String> map = new HashMap<>();
map.put(1, "第一个value");
map.put(2, "第二个value");
map.put(3, "第三个value");
//通过keySet取出map数据[for-each循环]
Set<Integer> keys = map.keySet();
for(Integer key:keys){
System.out.println("key值:"+key+" value值:"+map.get(key));
}
//通过EntrySet取出map数据[for-each循环]
Set<Entry<Integer, String>> entrys = map.entrySet();
for(Entry<Integer, String> entry:entrys){
System.out.println("key值:"+entry.getKey()+" value值:"+entry.getValue());
}
6.迭代器中删除元素
Iterator it=list.iterator();
while(it.hasNext()){
Object e=it.next();
if("b".equals(e)){
it.remove();
}
}
7.常见的日期操作(取值、转换、加减、比较)
7.1:获取年月日
Calendar now = Calendar.getInstance();
int
year = now.get(Calendar.YEAR);
//,当前年份
int
month = now.get(Calendar.MONTH)+1 ;
//当前月
int
day = now.get(Calendar.DATE); //
当前日
7.2:自定义年月日
//只设定某个字段的值 ,et(int field, int value)
now.set(Calendar.YEAR, );
//设定年月日或者年月日时分或年月日时分秒,set(int year, int month, int date[, int hourOfDay, int minute, int second])
now.set(, , ,[, , , ]);
//直接传入一个 Date 类型的日期
now.set(date);
7.3:日期转换
SimpleDateFormat sdf =
new
SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss"
);
try
{
//日期转字符串
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
String dateStringParse = sdf.format(date);
//字符串转日期
String dateString =
"-- ::"
;
Date dateParse = sdf.parse(dateString);
}
catch
(ParseException e) {
e.printStackTrace();
}
7.4:日期加减
Calendar now = Calendar.getInstance();
now.add(Calendar.YEAR, );
//现在时间的年后
now.add(Calendar.YEAR, -);
//现在时间的年前
7.5:比较
SimpleDateFormat sdf =
new
SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss"
);
String dateString_01 =
"2016-01-01 11:11:11"
;
String dateString_02 =
"2016-01-02 11:11:11"
;
try
{
Date date_01 = sdf.parse(dateString_01);
Date date_02 = sdf.parse(dateString_02);
System.out.println(date_01.before(date_02));
//true,当 date_01 小于 date_02 时,为 true,否则为 false
System.out.println(date_02.after(date_01));
//true,当 date_02 大于 date_01 时,为 true,否则为 false
System.out.println(date_01.compareTo(date_02));
//-1,当 date_01 小于 date_02 时,为 -1
System.out.println(date_02.compareTo(date_01));
//1,当 date_02 大于 date_01 时,为 1
System.out.println(date_02.compareTo(date_02));
//0,当两个日期相等时,为 0
}
catch
(ParseException e) {
e.printStackTrace();
}
8.
DateUtils常用方法
isSameDay(final Date date1, final Date date2)
:判断两个时间是否是同一天;isSameInstant(final Date date1, final Date date2)
:判断两个时间是否为同一毫秒
9.BigDecimal加减乘除运算
BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2));
b1.add(b2).doubleValue();
b1.subtract(b2).doubleValue();
b1.multiply(b2).doubleValue();
b1.divide(b2,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
10.小数点位数保留
double d = 0.200;
DecimalFormat df = new DecimalFormat("0.00");
System.out.println(df.format(d));
若是这种写法DecimalFormat df = new DecimalFormat("0.00"),不管传入的任何值,均保留两位小数
double d = 1.000;
BigDecimal bd=new BigDecimal(d);
double d1=bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println(d1);
使用这种写法若小数点后均为零,则保留一位小数,并且有四舍五入的规则
11.枚举demo
public enum Color {
RED(0,"红色"),
BLUE(1,"这是蓝色"),
YELLOW(2,"这是黄色"),
GREEN(3,"这是绿色");
//可以看出这在枚举类型里定义变量和方法和在普通类里面定义方法和变量没有什么区别。
//唯一要注意的只是变量和方法定义必须放在所有枚举值定义的后面,否则编译器会给出一个错误。
private int code;
private String desc;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
Color(int code,String desc){ //加上public void 上面定义枚举会报错 The constructor Color(int, String) is undefined
this.code=code;
this.desc=desc;
}
/**
* 自己定义一个静态方法,通过code返回枚举常量对象
* @param code
* @return
*/
public static Color getValue(int code){
for (Color color : values()) {
if(color.getCode()== code){
return color;
}
}
return null;
}
}