java 将当前时间转换成数字、
时间: 2024-05-11 22:19:16 浏览: 11
Java中可以使用时间戳(timestamp)来表示当前时间的数字形式。时间戳是自1970年1月1日00:00:00 UTC以来的秒数。
以下是将当前时间转换成时间戳的Java代码示例:
```
import java.time.Instant;
public class Main {
public static void main(String[] args) {
Instant timestamp = Instant.now();
long epochSeconds = timestamp.getEpochSecond();
System.out.println("当前时间的时间戳为:" + epochSeconds);
}
}
```
输出结果类似于:
```
当前时间的时间戳为:1625513587
```
其中,`Instant.now()`方法获取当前时间,`getEpochSecond()`方法获取时间戳的秒数。
相关问题
Java打印当前时间按照全部数字打印包括毫秒
可以使用Java中的SimpleDateFormat类来实现。
代码如下:
```java
import java.text.SimpleDateFormat;
import java.util.Date;
public class PrintTime {
public static void main(String[] args) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String currentTime = dateFormat.format(new Date());
System.out.println(currentTime);
}
}
```
输出结果类似于:
```
20200815150349123
```
其中,SimpleDateFormat中的格式化字符串"yyyyMMddHHmmssSSS"代表年月日时分秒毫秒,对应的数字将被格式化输出。new Date()表示当前时间,dateFormat.format()方法将Date类型的时间转换为指定格式的字符串,最后将字符串打印输出即可。
java 将数字转换为人名币大写
以下是Java代码将数字转换为人民币大写的方法:
```java
import java.text.DecimalFormat;
public class RMBConverter {
private static final String[] CN_NUM = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
private static final String[] CN_UNIT = {"", "拾", "佰", "仟"};
private static final String[] CN_SECTION = {"", "万", "亿", "万亿"};
public static String convert(double num) {
if (num == 0) {
return "零元整";
}
long integer = (long) num; // 整数部分
StringBuilder sb = new StringBuilder();
int sectionIndex = 0; // 记录当前处理的是第几个节权位
while (integer > 0) {
int section = (int) (integer % 10000); // 取最后四位
sb.insert(0, CN_SECTION[sectionIndex]); // 插入节权位
sb.insert(0, sectionToChinese(section)); // 插入节内数字
integer /= 10000; // 去掉已经处理的最后四位
sectionIndex++; // 移动到下一个节权位
}
if (num < 0) {
sb.insert(0, "负");
}
DecimalFormat decimalFormat = new DecimalFormat("#.00");
String decimal = decimalFormat.format(num - (long) num); // 小数部分
if (decimal.equals("0.00")) {
sb.append("整");
} else {
sb.append(decimal.substring(2)).append("分");
sb.insert(0, decimalToChinese(decimal.substring(0, 2)) + "角");
}
return sb.toString();
}
private static String sectionToChinese(int section) {
StringBuilder sb = new StringBuilder();
if (section == 0) {
return CN_NUM[0];
}
int unitIndex = 0; // 记录当前处理的是第几位
boolean zero = true; // 记录当前位是否为零
while (section > 0) {
int digit = section % 10; // 取最后一位
if (digit == 0) {
if (!zero) {
sb.insert(0, CN_NUM[0]); // 插入零
zero = true;
}
} else {
sb.insert(0, CN_UNIT[unitIndex]); // 插入单位
sb.insert(0, CN_NUM[digit]); // 插入数字
zero = false;
}
section /= 10; // 去掉已经处理的最后一位
unitIndex++; // 移动到下一位
}
return sb.toString();
}
private static String decimalToChinese(String decimal) {
StringBuilder sb = new StringBuilder();
if (decimal.equals("00")) {
return "";
}
int digit1 = decimal.charAt(0) - '0';
int digit2 = decimal.charAt(1) - '0';
if (digit1 > 0) {
sb.append(CN_NUM[digit1]).append("拾");
} else if (digit2 > 0) {
sb.append("零");
}
if (digit2 > 0) {
sb.append(CN_NUM[digit2]);
}
return sb.toString();
}
}
```
使用方法如下:
```java
double num = 123456789.12;
String rmb = RMBConverter.convert(num);
System.out.println(rmb); // 壹亿贰仟叁佰肆拾伍万陆仟柒百捌十玖元壹角贰分
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)