目录
一、int转String
public class IntegerDemo2 {
public static void main(String[] args) {
int num=100;
//1
String s1=""+num;
System.out.println(s1);
//2
String s2 =String.valueOf(num);
System.out.println(s2);
//3
Integer i =new Integer(num);
String s3 =i.toString();
System.out.println(s3);
//4
String s4 =Integer.toString(i);
System.out.println(s4);
}
}
二、String转int
String s="100";
Integer ii =new Integer(s);
int x=ii.intValue();
System.out.println(x);
int y = Integer.parseInt(s);
System.out.println(y);
三、char转int
char ch = '9';
if (Character.isDigit(ch)){ // 判断是否是数字
int num = Integer.parseInt(String.valueOf(ch));
System.out.println(num);
}
char ch = '9';
if (Character.isDigit(ch)){ // 判断是否是数字
int num = (int)ch - (int)('0');
System.out.println(num);
}
四、String 转 double
五、String 转 Float
六、String为Long
注意,当String为Long数据类型时,即String长度超过int的长度时转换int数据类型时会出现错误的结果
七、String与Date互转
package com.god.genius.basic.jdk8.date;
import org.junit.jupiter.api.Test;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
/**
* @author liSir
* @date 2020/5/13
* @desp jdk8日期各种用法及各种转换使用
*/
public class DateDemo {
public static void main(String[] args) {
//1.时间日期
getDateTime();
//2.LocalDateTime与String互转
timeToString();
//3.Date与LocalDateTime互转
dateToLocalDateTime();
//4.时间运算
demo4();
//6.时间戳、瞬时点、Date、本地时间、转换
demo7();
}
private static void demo4() {
LocalDateTime ldt = LocalDateTime.now();
//-----获取指定单位的值:2020
int year = ldt.getYear();
//-----MAY,5
Month month = ldt.getMonth();
int value = month.getValue();
//140
int dayOfYear = ldt.getDayOfYear();
//-----19
int dayOfMonth = ldt.getDayOfMonth();
//TUESDAY,2
DayOfWeek dayOfWeek = ldt.getDayOfWeek();
int weekValue = dayOfWeek.getValue();
//-----9
int hour = ldt.getHour();
//-----18
int minute = ldt.getMinute();
//-----59
int second = ldt.getSecond();
//2020-06-03T17:59:00.123
//指定时间单位的值 2022-06-12T17:58:21.040
LocalDateTime localDateTime = ldt.withDayOfMonth(12).withYear(2022);
//在现有时间上做加法 2021-05-03T17:59:42.689
LocalDateTime localDateTime1 = ldt.plusYears(1).plusMonths(-1);
// 在现有时间上做减法
LocalDateTime ldt2 = LocalDateTime.now().minus(-2, ChronoUnit.MONTHS).minusDays(3);
// 获取一天的开始或结束
LocalDateTime ldtStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
LocalDateTime ldtEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
// 时间是否在指定时间之前
boolean isBefore = ldt.isBefore(ldt2);
// 时间是否在指定时间之后
boolean isAfter = ldt.isAfter(ldt2);
// 比较两个日期是否相等 重写的equals可以直接比较
boolean equality = ldt.equals(ldt2);
// 比较是否是周期性日期,比如 生日 节假日 账单日 等
MonthDay holiday = MonthDay.of(5, 1); // 五一
boolean xx = holiday.equals(MonthDay.from(LocalDateTime.now())); // 今天是否是五一
System.out.println(0);
}
/**
* 测试数据转换及封装工具的使用
*/
@Test
public void convert() {
/*Date date = DateUtils.parseDate("2018-08-01 21:22:22", DateUtils.DATE_YMDHMS);
LocalDateTime localDateTime = DateUtils.dateConvertToLocalDateTime(date);
Long localDateTimeSecond = localDateTime.toEpochSecond(ZoneOffset.of("+8"));
Long dateSecond = date.toInstant().atOffset(ZoneOffset.of("+8")).toEpochSecond();
Assert.assertTrue(dateSecond.equals(localDateTimeSecond));*/
}
private static void demo7() {
LocalDateTime now = LocalDateTime.now();
//1.获取秒数,毫秒数
long l = now.toEpochSecond(ZoneOffset.of("+8"));
long l1 = now.toInstant(ZoneOffset.of("+8")).toEpochMilli();
System.out.println(l);
System.out.println(l1);
}
private static void dateToLocalDateTime() {
Date date = new Date();
LocalDateTime localDateTime = dateToLocalDateTime(date);
System.out.println("localDateTime->" + localDateTime);
Date date2 = localDateTimeToDate(LocalDateTime.now());
System.out.println(date2);
}
private static Date localDateTimeToDate(LocalDateTime now) {
return Date.from(now.toInstant(ZoneOffset.of("+8")));
}
private static LocalDateTime dateToLocalDateTime(Date date) {
return date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
}
private static void timeToString() {
LocalDateTime now = LocalDateTime.now();
//1.LocalDateTime与String互转
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
String dateTime = now.format(formatter2);
System.out.println(dateTime);
//字符串转时间,字符串必须带时分秒
String dateTimeStr = "2020-07-28 14:11:15";
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime2 = LocalDateTime.parse(dateTimeStr, df);
System.out.println(dateTime2);
}
private static void getDateTime() {
//本地时间 09:07:12.976
LocalTime lt = LocalTime.now();
//本地日期 2020-05-19
LocalDate ld = LocalDate.now();
//本地日期时间 2020-05-19T09:08:07.727
LocalDateTime now = LocalDateTime.now();
//创建一个指定的时间 2012-02-12T12:12:12
LocalDateTime ldt = LocalDateTime.of(2012, 2, 12, 12, 12, 12);
//日期时间转日期或时间 2012-02-12
LocalDate localDate = ldt.toLocalDate();
//09:18:59.746
LocalTime localTime = ldt.toLocalTime();
}
}
八、使用正则表达式判断String是否为int整型or浮点型数据。
动态选择方法转换数据
九、判断字符串是否为数字
//方法一:用JAVA自带的函数
public static boolean isNumeric(String str){
for (int i = str.length();--i>=0;){
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
/*方法二:推荐,速度最快
* 判断是否为整数
* @param str 传入的字符串
* @return 是整数返回true,否则返回false
*/
public static boolean isInteger(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
return pattern.matcher(str).matches();
}
//方法三:
public static boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
//方法四:
public final static boolean isNumeric(String s) {
if (s != null && !"".equals(s.trim()))
return s.matches("^[0-9]*$");
else
return false;
}
//方法五:用ascii码
public static boolean isNumeric(String str){
for(int i=str.length();--i>=0;){
int chr=str.charAt(i);
if(chr<48 || chr>57)
return false;
}
return true;
}