Java数字与日期操作全解析
立即解锁
发布时间: 2025-08-18 02:16:49 阅读量: 3 订阅数: 7 

### Java 数字与日期操作全解析
在 Java 编程中,数字和日期的处理是非常常见且重要的任务。本文将详细介绍 Java 中数字和日期的相关操作,包括四舍五入、格式化、比较、随机数生成以及日期获取等内容。
#### 1. 浮点数和双精度数四舍五入为整数
在应用程序中,有时需要将浮点数或双精度数四舍五入为整数。可以使用 `java.lang.Math` 类的 `round()` 方法来实现。
```java
public static int roundFloatToInt(float myFloat){
return Math.round(myFloat);
}
public static long roundDoubleToLong(double myDouble){
return Math.round(myDouble);
}
public static void main(String[] args){
Float floatValue = 7.82f;
Double doubleValue = 9.9d;
System.out.println(roundFloatToInt(floatValue));
System.out.println(roundDoubleToLong(doubleValue));
}
```
上述代码中,`roundFloatToInt()` 方法接受一个浮点数并将其四舍五入为 `int` 类型,`roundDoubleToLong()` 方法接受一个双精度数并将其四舍五入为 `long` 类型。运行结果如下:
```
8
10
```
`Math` 类的 `round()` 方法会将浮点数或双精度数四舍五入到最接近的整数。如果参数是 `NaN`,则返回 0;如果参数是正无穷或负无穷,则分别返回 `Integer.MAX_VALUE` 或 `Integer.MIN_VALUE`(对于 `float` 类型),以及 `Long.MAX_VALUE` 或 `Long.MIN_VALUE`(对于 `double` 类型)。
需要注意的是,`NaN`、`POSITIVE_INFINITY` 和 `NEGATIVE_INFINITY` 是 `Float` 和 `Double` 类中定义的常量。`NaN` 表示非数字,例如 `0.0f / 0.0f` 会产生 `NaN` 值;`POSITIVE_INFINITY` 和 `NEGATIVE_INFINITY` 分别表示正无穷和负无穷,例如 `1.0 / 0.0` 会产生正无穷,`-1.0 / 0.0` 会产生负无穷。
以下代码可以测试这些特殊值:
```java
public static void main(String[] args){
Float floatValue1 = 1.0f;
Float floatValue1n = -1.0f;
Float floatValue0 = 0.0f;
System.out.println(floatValue1/floatValue0);
System.out.println(floatValue0/floatValue0);
System.out.println(floatValue1n/floatValue0);
Double doubleValue1 = 1.0d;
Double doubleValue1n = -1.0d;
Double doubleValue0 = 0.0d;
System.out.println(doubleValue1/doubleValue0);
System.out.println(doubleValue0/doubleValue0);
System.out.println(doubleValue1n/doubleValue0);
}
```
输出结果为:
```
Infinity
NaN
-Infinity
Infinity
NaN
-Infinity
```
#### 2. 格式化双精度数和长整型小数
在应用程序中,可能需要对双精度数和长整型数进行格式化。可以使用 `DecimalFormat` 类来实现。
```java
public static void formatDouble(double myDouble){
NumberFormat numberFormatter = new DecimalFormat("##.000");
String result = numberFormatter.format(myDouble);
System.out.println(result);
}
public static void main(String[] args) {
formatDouble(Double.valueOf("345.9372"));
}
```
如果传入 `formatDouble()` 方法的双精度数是 `345.9372`,结果将是 `345.937`;如果传入 `.7697`,结果将是 `.770`。
`DecimalFormat` 类可以与 `NumberFormat` 类一起使用,用于对双精度数或长整型数进行四舍五入和/或格式化。`NumberFormat` 是一个抽象类,提供了格式化和解析数字的接口。
以下是一些使用 `NumberFormat` 类工厂方法的示例:
```java
// Obtains an instance of NumberFormat class
NumberFormat format = NumberFormat.getInstance();
// Format a double value for the current locale
String result = format.format(83.404);
System.out.println(result);
// Format a double value for an Italian locale
result = format.getInstance(Locale.ITALIAN).format(83.404);
System.out.println(result);
// Parse a String into a Number
try {
Number num = format.parse("75.736");
System.out.println(num);
} catch (java.text.ParseException ex){
System.out.println(ex);
}
```
输出结果为:
```
Current Locale: 83,404
Italian Locale: 83,404
Now a number: 75736
```
#### 3. 紧凑数字格式化
如果想以简短或易读的形式表示一个数字,可以使用 Java 12 引入的 `CompactNumberInstance` 方法。
```java
public static void main (String[] args) {
NumberFormat numberFormat = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
String result = numberFormat.format(1000);
System.out.println(result);
}
```
输出结果为:
```
1k
```
#### 4. 比较 int 值
比较两个或多个 `int` 值有两种方法。
**方法一:使用比较运算符**
```java
public static void compareIntegers(){
int int1 = 1;
int int2 = 10;
int int3 = -5;
System.out.println(int1 == int2); // Result: false
System.out.println(int3 == int1); // Re
```
0
0
复制全文
相关推荐










