1、基本数据类型的变量:

/**
* 1、基本数据类型的变量:
*
*   1)、整数类型:byte(1字节=8bit),short(2字节),int(4字节),long(8字节)
*
*   2)、浮点数类型:float(4字节),double(8字节)
*
*   3)、字符类型:char(2字节)----Unicode码
*
*   4)、布尔类型:boolean(未指定字节数)----true和false
*
* 2、引用数据类型的变量:
*
*   除了基本类型(primitive type)和枚举类型(enumeration type),剩下的都是引用类型,比如String。
* */

(1)、Demo01: 数据取值范围的注意:

//静态方法不需要实例化
public static void basicDataDemo(){
/**
* 1、声明long型变量,必须以"l"或"L"结尾,其整数默认是int类型(易超出其取值范围而报错)
* 2、需注意数据的取值范围
* (1)、byte的取值范围:-128~127(-2^7~2^7-1)
* (2)、short的取值范围:-32768 ~ 32767(-2^15~2^15-1)
* (3)、int取值范围:-2147483648 ~ 2147483647(-2^31~2^31-1)
* (4)、。。。。。。
* 3、浮点型默认是双精度数,将双精度型(double)赋值给浮点型(float)属于下转型会造成精度损失,因此需要强制类型转换
* 4、整数默认是int类型
*/
//错误:
//long l = 2147483648;
long l = 2147483648L; //byte b=128;
byte b=28; //丢失精度,两种解决方案
//float f=1.2;
float f1=1.2f;
float f2=(float) 1.2; //丢失精度
//short s1 = 1;
//s1 = s1 + 1;
short s1 = 1;
short s2 = 1;
s1 = (short)(s1 + 1);
//有隐含的强制类型转换,推荐使用
s2 +=1; System.out.println(l); //2147483648
System.out.println(b); //28
System.out.println(f1); //1.2
System.out.println(f2); //1.2
System.out.println(s1); //2
System.out.println(s2); //2
}

(2)、Demo02: String引用数据类型的注意:


/**
* JVM的基本了解:
* 1、方法区内存:存储字节码代码片段、静态变量
* 2、堆内存:new实例变量、数组
* 3、栈内存:局部变量
* */

//静态方法不需要实例化
    public static void stringDataDemo(){
/**
* 1、String s1 = "123";
* 在静态区的“123”(字符串常量池)创建了一个对象
* 2、String s2 = new String("123");
* 创建了二个对象:一个在静态区的“123”(字符串常量池)对象,一个是用new创建在堆上的对象
* */
String s1 = "123";
String s2 = new String("123");
System.out.println(s1 == s2); //false String s3 = "123456";
String s4 = "123" + "456";
System.out.println(s3 == s4); //true String s5 = s1 + "456";
System.out.println(s5 == s4); //false /**
* String与Stringbuffer/ Stringbulider的区别:
*
* String类型是只读字符串,字符串内容不允许改变
* Stringbuffer/ Stringbulider表示字符串对象可以修改
* 1、 Stringbuffer:有synchronized关键字修饰,表示线程安全的,效率低
* 2、 Stringbulider:无synchronized关键字修饰,表示非线程安全的,效率高
* 开发建议使用:Stringbuffer》Stringbulider》String
* */
String a = new String("123");
StringBuffer b = new StringBuffer("123");
b.append("456"); //字符串a不允许修改
System.out.println("a:"+ a); //a:123
System.out.println("b:"+ b); //b:123456
}

2、包装类:

/**
* 包装类:
* 1、基本包装类
* Boolean
* Byte
* Short
* Character
* Long
* Integer
* Float
* Double
*
* 2、高精度数字
* BigDecimal
* BigInteger
* */

(1)、Demo03: 数据间转换的注意:

/**
* 1、自动类型转换
* byte 、char 、short --> int --> long --> float --> double
*
* 2、强制类型转换
* 使用强转符:(),可能导致精度损失
* */

//静态方法不需要实例化
public static void dataConversionDemo(){
/**
* 数据转换工具类:DataConversionUtil
* 1、自动装箱:自动将基本数据类型转换为包装器类型
* 2、自动拆箱:自动将包装器类型转换为基本数据类型
* 3、声明包装类可以避免空指针异常
* */
String a ="123";
int b;
int c;
//利用包装类实现自动装箱与自动拆箱,即数据类型转换
b = Integer.parseInt(a);
//调用工具类的静态方法
c = DataConversionUtil.toInt(a);
System.out.println(b); //123
System.out.println(c); //123 int d =123;
String e ;
String f ;
e = DataConversionUtil.toStr(d);
f = String.valueOf(d);
System.out.println(e); //123
System.out.println(f); //123 //避免空指针异常
Integer i = null;
System.out.println(i); //null
//报错
//int j = null;
//System.out.println(j);
}

(2)、数据转换工具类:

DataConversionUtil.class

1)、转换为数组:

   /**
* 转换为Integer数组<br>
*
* @param str 被转换的值
* @return 结果
*/
public static Integer[] toIntArray(String str) {
return toIntArray(",", str);
} /**
* 转换为Long数组<br>
*
* @param str 被转换的值
* @return 结果
*/
public static Long[] toLongArray(String str) {
return toLongArray(",", str);
} /**
* 转换为Integer数组<br>
*
* @param split 分隔符
* @param split 被转换的值
* @return 结果
*/
public static Integer[] toIntArray(String split, String str) {
if (StringUtils.isEmpty(str)) {
return new Integer[]{};
}
String[] arr = str.split(split);
final Integer[] ints = new Integer[arr.length];
for (int i = 0; i < arr.length; i++) {
final Integer v = toInt(arr[i], 0);
ints[i] = v;
}
return ints;
} /**
* 转换为Long数组<br>
*
* @param split 分隔符
* @param str 被转换的值
* @return 结果
*/
public static Long[] toLongArray(String split, String str) {
if (StringUtils.isEmpty(str)) {
return new Long[]{};
}
String[] arr = str.split(split);
final Long[] longs = new Long[arr.length];
for (int i = 0; i < arr.length; i++) {
final Long v = toLong(arr[i], null);
longs[i] = v;
}
return longs;
} /**
* 转换为String数组<br>
*
* @param str 被转换的值
* @return 结果
*/
public static String[] toStrArray(String str) {
return toStrArray(",", str);
} /**
* 转换为String数组<br>
*
* @param split 分隔符
* @param split 被转换的值
* @return 结果
*/
public static String[] toStrArray(String split, String str) {
return str.split(split);
}

2)、转换为Enum对象:

  /**
* 转换为Enum对象<br>
* 如果给定的值为空,或者转换失败,返回默认值<br>
*
* @param clazz Enum的Class
* @param value 值
* @param defaultValue 默认值
* @return Enum
*/
public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value, E defaultValue) {
if (value == null) {
return defaultValue;
}
if (clazz.isAssignableFrom(value.getClass())) {
@SuppressWarnings("unchecked")
E myE = (E) value;
return myE;
}
final String valueStr = toStr(value, null);
if (StringUtils.isEmpty(valueStr)) {
return defaultValue;
}
try {
return Enum.valueOf(clazz, valueStr);
} catch (Exception e) {
return defaultValue;
}
} /**
* 转换为Enum对象<br>
* 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>
*
* @param clazz Enum的Class
* @param value 值
* @return Enum
*/
public static <E extends Enum<E>> E toEnum(Class<E> clazz, Object value) {
return toEnum(clazz, value, null);
}

3)、转换为BigInteger:

  /**
* 转换为BigInteger<br>
* 如果给定的值为空,或者转换失败,返回默认值<br>
* 转换失败不会报错
*
* @param value 被转换的值
* @param defaultValue 转换错误时的默认值
* @return 结果
*/
public static BigInteger toBigInteger(Object value, BigInteger defaultValue) {
if (value == null) {
return defaultValue;
}
if (value instanceof BigInteger) {
return (BigInteger) value;
}
if (value instanceof Long) {
return BigInteger.valueOf((Long) value);
}
final String valueStr = toStr(value, null);
if (StringUtils.isEmpty(valueStr)) {
return defaultValue;
}
try {
return new BigInteger(valueStr);
} catch (Exception e) {
return defaultValue;
}
} /**
* 转换为BigInteger<br>
* 如果给定的值为空,或者转换失败,返回默认值<code>null</code><br>
* 转换失败不会报错
*
* @param value 被转换的值
* @return 结果
*/
public static BigInteger toBigInteger(Object value) {
return toBigInteger(value, null);
}

4)、转换为BigDecimal:

  /**
* 转换为BigDecimal<br>
* 如果给定的值为空,或者转换失败,返回默认值<br>
* 转换失败不会报错
*
* @param value 被转换的值
* @param defaultValue 转换错误时的默认值
* @return 结果
*/
public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue) {
if (value == null) {
return defaultValue;
}
if (value instanceof BigDecimal) {
return (BigDecimal) value;
}
if (value instanceof Long) {
return new BigDecimal((Long) value);
}
if (value instanceof Double) {
return new BigDecimal((Double) value);
}
if (value instanceof Integer) {
return new BigDecimal((Integer) value);
}
final String valueStr = toStr(value, null);
if (StringUtils.isEmpty(valueStr)) {
return defaultValue;
}
try {
return new BigDecimal(valueStr);
} catch (Exception e) {
return defaultValue;
}
} /**
* 转换为BigDecimal<br>
* 如果给定的值为空,或者转换失败,返回默认值<br>
* 转换失败不会报错
*
* @param value 被转换的值
* @return 结果
*/
public static BigDecimal toBigDecimal(Object value) {
return toBigDecimal(value, null);
}

5)、全角半角转换:

  /**
* 半角转全角
*
* @param input String.
* @return 全角字符串.
*/
public static String toSBC(String input) {
return toSBC(input, null);
} /**
* 半角转全角
*
* @param input String
* @param notConvertSet 不替换的字符集合
* @return 全角字符串.
*/
public static String toSBC(String input, Set<Character> notConvertSet) {
char c[] = input.toCharArray();
for (int i = 0; i < c.length; i++) {
if (null != notConvertSet && notConvertSet.contains(c[i])) {
// 跳过不替换的字符
continue;
} if (c[i] == ' ') {
c[i] = '\u3000';
} else if (c[i] < '\177') {
c[i] = (char) (c[i] + 65248); }
}
return new String(c);
} /**
* 全角转半角
*
* @param input String.
* @return 半角字符串
*/
public static String toDBC(String input) {
return toDBC(input, null);
} /**
* 替换全角为半角
*
* @param text 文本
* @param notConvertSet 不替换的字符集合
* @return 替换后的字符
*/
public static String toDBC(String text, Set<Character> notConvertSet) {
char c[] = text.toCharArray();
for (int i = 0; i < c.length; i++) {
if (null != notConvertSet && notConvertSet.contains(c[i])) {
// 跳过不替换的字符
continue;
} if (c[i] == '\u3000') {
c[i] = ' ';
} else if (c[i] > '\uFF00' && c[i] < '\uFF5F') {
c[i] = (char) (c[i] - 65248);
}
}
String returnString = new String(c); return returnString;
}

6)、数字金额大写转换:

  /**
* 数字金额大写转换 先写个完整的然后将如零拾替换成零
*
* @param n 数字
* @return 中文大写数字
*/
public static String digitUppercase(double n) {
String[] fraction = {"角", "分"};
String[] digit = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
String[][] unit = {{"元", "万", "亿"}, {"", "拾", "佰", "仟"}}; String head = n < 0 ? "负" : "";
n = Math.abs(n); String s = "";
for (int i = 0; i < fraction.length; i++) {
s += (digit[(int) (Math.floor(n * 10 * Math.pow(10, i)) % 10)] + fraction[i]).replaceAll("(零.)+", "");
}
if (s.length() < 1) {
s = "整";
}
int integerPart = (int) Math.floor(n); for (int i = 0; i < unit[0].length && integerPart > 0; i++) {
String p = "";
for (int j = 0; j < unit[1].length && n > 0; j++) {
p = digit[integerPart % 10] + unit[1][j] + p;
integerPart = integerPart / 10;
}
s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;
}
return head + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");
}

3、日期类:

(1)、取得当前年、月、日:

  /**
* 取得当前年的年份
*
* @return int
*/
public static int getYear() {
Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.YEAR);
} /**
* 取得当前月的月份
*
* @return int
*/
public static int getMonth() {
Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.MONTH);
} /**
* 取得当前日的日期
*
* @return int
*/
public static int getDay() {
Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.DATE);
}

(2)、格式化时间,时间转字符串:

  /**
* 格式化时间,时间转字符串
*
* @param date null则为当前系统时间
* @param format 格式,null则默认为:'yyyy-MM-dd HH:mm:ss'
* @return 字符串格式的日期
*/
public static String getDateTimeByStr(Date date, String format) {
if (date == null) {
date = new Date();
}
if (format == null) {
format = "yyyy-MM-dd HH:mm:ss";
}
return new SimpleDateFormat(format).format(date);
}

(3)、获取两时间相隔月份:

  /**
* 两时间相隔月份
* @param startDate
* @param endDate
* @return
*/
public static Integer getDiffMonth(Date startDate, Date endDate){
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
start.setTime(startDate);
end.setTime(endDate);
int result = end.get(Calendar.MONTH) - start.get(Calendar.MONTH);
int month = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * 12;
return Math.abs(month + result);
}

(4)、获得特定日期之后的固定天数的某一天:

  /**
* @param date 传入的日期
* @param days 要增加的天数
* @return Date 获取的目标日期
*
* 获得特定日期之后的固定天数的某一天
*/
public static Date addDays(Date date, int days) {
Date date1 = null;
try {
if (null == date) {
return date1;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, days);
date1 = calendar.getTime();
} catch (Exception e) {
e.printStackTrace();
} finally {
return date1;
}
}

(5)、获得特定日期之前的固定天数的某一天:

    /**
* @param date 传入的日期
* @param days 要回退的天数
* @return Date 获取的目标日期
*
* 获得特定日期之前的固定天数的某一天
*/
public static Date getPastDate(Date date, int days) {
Date date1 = null;
try {
if (null == date) {
return date1;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - days);
date1 = calendar.getTime();
} catch (Exception e) {
e.printStackTrace();
} finally {
return date1;
}
}

(6)、获取日期相减后的天数:

  /**
* @param minuendDate 被减数日期
* @param subDate 减数日期
* @return int 相差的天数
*
* 获得日期相减后的天数
*/
public static int subDate(Date minuendDate, Date subDate) {
if (minuendDate != null && subDate != null) {
long timeMillions = (minuendDate.getTime() - subDate.getTime()) % (24 * 60 * 60 * 1000);
int days = new Long((minuendDate.getTime() - subDate.getTime()) / (24 * 60 * 60 * 1000)).intValue();
if (timeMillions == 0) {
return days;
} else {
return days + 1;
}
} else {
return 0;
} }

(7)、获取昨天凌晨后第i刻的时间:

    /**
* 获取昨天凌晨后第i刻的时间
* @return Date
* */
private static Date getYesterdayQuarter(int i) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.add(Calendar.DATE, -1); //得到前一天
calendar.add(Calendar.MINUTE, i * 15);
Date date = calendar.getTime();
return date;
}

(8)、获取当前时间对应的昨天第i刻时间:

    /**
* 获取当前时间对应的昨天第i刻时间
* @return Date
* */
private static Date getYesterdayDate(int i) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.add(Calendar.DATE, -1); //得到前一天
calendar.add(Calendar.MINUTE, i * 15);
Date date = calendar.getTime();
return date;
}

(9)、获取今天凌晨后第i刻的时间:

    /**
* 获取今天凌晨后第i刻的时间
* @return Date
* */
private static Date getNowQuarter(int i) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.add(Calendar.MINUTE, i * 15);
Date date = calendar.getTime();
return date;
}

(10)、计算两时间相差的分钟数:

    /**
* 计算两时间相差的分钟数
* @return long
* */
private static long getDifferenceMinute(Date startDate, Date endDate) {
long between = (endDate.getTime() - startDate .getTime())/1000;
long min = between/60;
return min;
}

搜索

复制

最新文章

  1. JavaScript随笔2
  2. 【数据库】 防止sql注入,过滤敏感关键字
  3. Python天猫淘宝评论爬虫
  4. 笔记:js的replace函数
  5. js数据结构与算法存储结构
  6. python import
  7. 自己动手写缓存Version1
  8. JavaScript中的面向对象程序设计
  9. 借助csv用PHP生成excel文件
  10. Spark算子--flatMapValues
  11. 【BZOJ1801】【AHOI2009】中国象棋(动态规划)
  12. Web API中的路由(一)——约定路由
  13. Java设计模式学习记录-中介者模式
  14. 编程实践:使用java访问mySQL数据库
  15. selinux 设置的彻底理解 并要 熟练经常的使用
  16. python 协程 demo
  17. [原]关于helios自定义面板简述
  18. ADHOC Report 配置
  19. 折叠菜单slideUp
  20. [js]正则篇

热门文章

  1. PTA2021 跨年挑战赛部分题解
  2. pgsql 的问题
  3. Java8新特性之Stream流(含具体案例)
  4. SpringBoot(七) - Redis 缓存
  5. 商品期货通用模型JF1
  6. Docker之介绍与安装
  7. maven-入门到入土
  8. scrapy传递 item时的 数据不匹配 和一些注意事项
  9. [C++] - GCC和LLVM对方法 warning: non-void function does not return a value [-Wreturn-type] 的处理差异
  10. Vue3 企业级优雅实战 - 组件库框架 - 6 搭建example环境