目录

一、int转String

二、String转int

三、char转int

四、String 转 double

五、String 转 Float

六、String为Long

七、String与Date互转

八、使用正则表达式判断String是否为int整型or浮点型数据

九、判断字符串是否为数字


一、int转String

public class IntegerDemo2 {
public static void main(String[] args) {int num=100;//1String s1=""+num;System.out.println(s1);//2String s2 =String.valueOf(num);System.out.println(s2);//3Integer i =new Integer(num);String s3 =i.toString();System.out.println(s3);//4String 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

Java各种数据类型互转-编程之家

五、String 转 Float

Java各种数据类型互转-编程之家

六、String为Long

注意,当String为Long数据类型时,即String长度超过int的长度时转换int数据类型时会出现错误的结果

Java各种数据类型互转-编程之家

七、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();//-----获取指定单位的值:2020int year = ldt.getYear();//-----MAY,5Month month = ldt.getMonth();int value = month.getValue();//140int dayOfYear = ldt.getDayOfYear();//-----19int dayOfMonth = ldt.getDayOfMonth();//TUESDAY,2DayOfWeek dayOfWeek = ldt.getDayOfWeek();int weekValue = dayOfWeek.getValue();//-----9int hour = ldt.getHour();//-----18int minute = ldt.getMinute();//-----59int second = ldt.getSecond();//2020-06-03T17:59:00.123//指定时间单位的值 2022-06-12T17:58:21.040LocalDateTime localDateTime = ldt.withDayOfMonth(12).withYear(2022);//在现有时间上做加法 2021-05-03T17:59:42.689LocalDateTime 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);}/*** 测试数据转换及封装工具的使用*/@Testpublic 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.976LocalTime lt = LocalTime.now();//本地日期 2020-05-19LocalDate ld = LocalDate.now();//本地日期时间 2020-05-19T09:08:07.727LocalDateTime now = LocalDateTime.now();//创建一个指定的时间 2012-02-12T12:12:12LocalDateTime ldt = LocalDateTime.of(2012, 2, 12, 12, 12, 12);//日期时间转日期或时间 2012-02-12LocalDate localDate = ldt.toLocalDate();//09:18:59.746LocalTime localTime = ldt.toLocalTime();}}

八、使用正则表达式判断String是否为int整型or浮点型数据。

动态选择方法转换数据

Java各种数据类型互转-编程之家

九、判断字符串是否为数字

//方法一:用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]*$");elsereturn 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;
}