吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2478|回复: 17
收起左侧

[Java 转载] java中常用的的方法

[复制链接]
yuluo829 发表于 2022-2-5 22:38

java中常用的的方法

Integer类中的方法

parseInt(String s)

// 将字符串参数解析为带符号的十进制整数
String s = "123";
System.out.println(Integer.parseInt(s));  // 123

toBinaryString(int i)`

// 在基数2中返回整数参数的字符串表示形式为无符号整数
int num = 3;
System.out.println("2进制" + Integer.toBinaryString(num));

toHexString(int i)

// 返回整数参数的字符串表示形式,作为16位中的无符号整数
System.out.println("8进制" + Integer.toOctalString(num));

toOctalString(int i)

// 在基数8中返回整数参数的字符串表示形式为无符号整数
System.out.println("16进制" + Integer.toHexString(num));

String int Integer互相转换的方法

int -> String

数字 + '', String.valueOf(int i)

String -> int

Integer.parseInt(String s)

int -> Integer

自动装箱, Integer.valueOf(int i)

Integer -> int

自动拆箱,intValue(Integer i)

String -> Integer

Integer.valueOf(String s)

Integer -> String

String.valueOf(Integer i)

java中对日期的处理

构造器

// 获取系统当前时间
Date nowTime = new Date();
System.out.println(nowTime);

日期格式化

Date -> String

SimpleDateFormat类在java.text包下

// 其中一个构造方法
SimpleDateFormat(String pattern)

 使用给定模式 `SimpleDateFormat`并使用默认的 [`FORMAT`](../../java/util/Locale.Category.html#FORMAT)语言环境的默认日期格式符号`
  • 指定的字符串格式

  • >
    >
    > | Date and Time Pattern            | Result                                 |
    > | -------------------------------- | -------------------------------------- |
    > | "yyyy.MM.dd G 'at' HH:mm:ss z" | 2001.07.04 AD at 12:08:56 PDT        |
    > | "EEE, MMM d, ''yy"             | Wed, Jul 4, '01                      |
    > | "h:mm a"                       | 12:08 PM                             |
    > | "hh 'o''clock' a, zzzz"        | 12 o'clock PM, Pacific Daylight Time |
    > | "K:mm a, z"                    | 0:08 PM, PDT                         |
    > | "yyyyy.MMMMM.dd GGG hh:mm aaa" | 02001.July.04 AD 12:08 PM            |
    > | "EEE, d MMM yyyy HH:mm:ss Z"   | Wed, 4 Jul 2001 12:08:56 -0700       |
    > | "yyMMddHHmmssZ"                | 010704120856-0700                    |
    > | "yyyy-MM-dd'T'HH:mm:ss.SSSZ"   | 2001-07-04T12:08:56.235-0700         |
    > | "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" | 2001-07-04T12:08:56.235-07:00        |
    > | "YYYY-'W'ww-u"                 | 2001-W27-3                           |

// 日期格式化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
String date = sdf.format(nowTime);
System.out.println(date);   // 2022-02-05:13:29:22

String -> Date

// String -> Date
String date1 = "2020-01-01:12:12";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd:HH:mm");
Date date2 = sdf1.parse(date1);
System.out.println(date2); // Wed Jan 01 12:12:00 CST 2020

记录程序的运行时间

// 获取执行前的毫秒数
long begin = System.currentTimeMillis();

// 要检测的代码块

// 获取执行后的毫秒数
long end = System.currentTimeMillis();

System.out.println("耗费时长" + (end - begin) + "毫秒");

System;类中的常用方法

System.out   [out是System类的静态变量]
System.out.println()  [println()是PrintStream类的的方法]
System.gc() 建议启动垃圾回收期
System.currentTimeMillies() 获取自1970年00时00分00秒 000毫秒到今的总毫秒数
System.exit(0) 退出jvm虚拟机

数字格式化

// DecimalFormat专门负责数字格式化
// DecimalFormat df = new DecimalFormat("数字格式");
  • 数字格式
    • # 代表任意数字
    • , 代表千分位
    • . 代表小数点
    • 0 代表不够时补0
double number = 1234.56;
DecimalFormat df = new DecimalFormat("###,###.##");
String data = df.format(number);

System.out.println(data);  // 1,234.56

免费评分

参与人数 2热心值 +2 收起 理由
dadaliya + 1 鼓励转贴优秀软件安全工具和文档!
lgc81034 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

kmkim 发表于 2022-2-10 14:48
这种帖子不存在灌水嫌疑吗?
ppmmaiwo 发表于 2022-7-29 17:44
谢谢楼主分享
日期格式化建议用DateTimeFormat
SimpleDateFormat 线程不安全
DateTimeFormat 线程安全
破凤凰 发表于 2022-2-6 07:12
头像被屏蔽
cmbslgn 发表于 2022-2-6 08:53
提示: 作者被禁止或删除 内容自动屏蔽
13894541733 发表于 2022-2-6 09:10
感谢老师分享
Moon。 发表于 2022-2-6 13:39

谢谢楼主分享
xiaolong620 发表于 2022-2-28 10:01
谢谢楼主分享
MOEYU_VANILLA 发表于 2022-2-28 10:03
感谢分享!
denweiaa 发表于 2022-5-13 17:37

谢谢楼主分享
dgzw 发表于 2022-7-8 06:53
感谢分享,非常的好
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-25 15:58

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表