目录
- 关于java时间日期格式的24小时和12小时
- 把数字转换成时间
关于java时间日期格式的24小时和12小时
- 格式里的时如果用hh表示用12小时制,HH表示用24小时制。
- 例如:
- yyyy-MM-dd hh:mm:ss 12小时
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”); //小写是12小时 - yyy-MM-dd HH:MM:ss 24小时
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:MM:ss”);//大写是24小时的
- yyyy-MM-dd hh:mm:ss 12小时
把数字转换成时间
-
时间转换,拿到一串数字的时候,可以先到这个网站核对下时间戳是否正确。
-
在Java里面,用的是毫秒算的,因此,用Long类型的话,需要先把数字转换成Long类型再转成时间。
-
上代码:
-
1625656283000 :毫秒算,是2021-07-07 19:11:23
-
1625656283:毫秒算,是 1970-01-20 03:34:16
-
1秒=1000毫秒
import static org.junit.Assert.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.junit.Test;
public class test1108 {
@Test
public void test() {
Date date = new Date();
//格式里的时如果用hh表示用12小时制,HH表示用24小时制。MM必须是大写!
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
date.setTime(Long.parseLong("1625656283000"));//java里面应该是按毫秒
System.out.println(sdf.format(date));
date.setTime(1625656283);
System.out.println(sdf.format(date));
}
}
运算结果:
2018-11-08 11:24:22
1970-01-19 04:14:07
转自:https://blog.csdn.net/qq_36411874/article/details/83862957