时间操作(Java版)—将毫秒转换为年月日时分秒
第一种方式:
import java.util.Calendar;
import java.util.TimeZone;public class Test {/*** 将毫秒转换为年月日时分秒* * @author GaoHuanjie*/public String getYearMonthDayHourMinuteSecond(long timeMillis) {Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8")); calendar.setTimeInMillis(timeMillis);int year=calendar.get(Calendar.YEAR);int month=calendar.get(Calendar.MONTH) + 1;String mToMonth=null;if (String.valueOf(month).length()==1) {mToMonth="0"+month;} else {mToMonth=String.valueOf(month);}int day=calendar.get(Calendar.DAY_OF_MONTH);String dToDay=null;if (String.valueOf(day).length()==1) {dToDay="0"+day;} else {dToDay=String.valueOf(day);}int hour=calendar.get(Calendar.HOUR_OF_DAY);String hToHour=null;if (String.valueOf(hour).length()==1) {hToHour="0"+hour;} else {hToHour=String.valueOf(hour);}int minute=calendar.get(Calendar.MINUTE);String mToMinute=null;if (String.valueOf(minute).length()==1) {mToMinute="0"+minute;} else {mToMinute=String.valueOf(minute);}int second=calendar.get(Calendar.SECOND);String sToSecond=null;if (String.valueOf(second).length()==1) {sToSecond="0"+second;} else {sToSecond=String.valueOf(second);}return year+ "-" +mToMonth+ "-" +dToDay+ " "+hToHour+ ":" +mToMinute+ ":" +sToSecond; }public static void main(String[] args) {System.out.println(new Test().getYearMonthDayHourMinuteSecond(System.currentTimeMillis()));}
}
第二种方式:
public class Test {/*** 将毫秒转换为年月日时分秒* * @author GaoHuanjie*/public String getYearMonthDayHourMinuteSecond(long timeMillis) {int timezone = 8; // 时区long totalSeconds = timeMillis / 1000;totalSeconds += 60 * 60 * timezone;int second = (int) (totalSeconds % 60);// 秒long totalMinutes = totalSeconds / 60;int minute = (int) (totalMinutes % 60);// 分long totalHours = totalMinutes / 60;int hour = (int) (totalHours % 24);// 时int totalDays = (int) (totalHours / 24);int _year = 1970;int year = _year + totalDays / 366;int month = 1;int day = 1;int diffDays;boolean leapYear;while (true) {int diff = (year - _year) * 365;diff += (year - 1) / 4 - (_year - 1) / 4;diff -= ((year - 1) / 100 - (_year - 1) / 100);diff += (year - 1) / 400 - (_year - 1) / 400;diffDays = totalDays - diff;leapYear = (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);if (!leapYear && diffDays < 365 || leapYear && diffDays < 366) {break;} else {year++;}}int[] monthDays;if (diffDays >= 59 && leapYear) {monthDays = new int[] { -1, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };} else {monthDays = new int[] { -1, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };}for (int i = monthDays.length - 1; i >= 1; i--) {if (diffDays >= monthDays[i]) {month = i;day = diffDays - monthDays[i] + 1;break;}}return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;}public static void main(String[] args) {System.out.println(new Test().getYearMonthDayHourMinuteSecond(System.currentTimeMillis()));}
}
时间操作(Java版)—将毫秒转换为年月日时分秒相关推荐
- 将毫秒转换为年月日时分秒
translateTime(time) {//将毫秒转换为年月日时分秒 if (!time) { return ""; } else { l ...
- 时间格式2020-09-29T18:02:02.000Z转换为“年月日时分秒“
遇到一个需求: 选择2020-09-30 02:02:02时间后打印出来是 "2020-09-29T18:02:02.000Z" 格式的,后台需要转换为 "2020-09 ...
- 东八区时间转换为年月日时分秒(2020-10-26T11:34:30Z转换为2020-10-26 11:34:30)
将东八区时间转换为年月日时分秒 //直接上demofunction rTime(date) {var json_date = new Date(date).toJSON();return new Da ...
- vue怎么截取时间年月_时间格式的转化 vue与js 年月日 时分秒
首先使用原生转化的方法 第一种 //时间转换 dateStr(d, sign) { //如果没有传递符号,给一个默认的符号 if (!sign) { sign = '-' } //获取d里面年月日时分 ...
- 中国标准时间转换为年月日时分秒,指定00:00:00 或23:59:59或当下时分秒
每次写时间转化问题都好痛苦,花费巨大时间理清关系后,下次再写又忘了.好记性不如烂笔头,记录下来,方便下次翻阅. 1.中国标准时间转换为标准年月日时分秒的调用方法如下 export function f ...
- 将中国标准时间转换为年月日时分秒格式
1.将中国标准时间转换为年月日时分秒格式 代码如下(示例): // 时间格式化timestampToTime(timestamp) {var chinaStandard=Mon Jul 19 2021 ...
- js获取时间戳,将时间戳转换为年月日时分秒
一.js获取时间戳 // js获取当前时间戳(三种方式都可以)let time1 = new Date().getTime();let time2 = Date.now();let time3 = n ...
- 日月年时分秒转换为年月日时分秒
//日月年时分秒转化年月日时分秒changeTime(ss) {var form_date_value = ss.split("-");jie = form_date_value[ ...
- java date加一天_Java日期时间API系列15-----Jdk8中API类,java日期计算2,年月日时分秒的加减等...
通过Java日期时间API系列8-----Jdk8中java.time包中的新的日期时间API类的LocalDate源码分析 ,可以看出java8设计非常好,实现接口Temporal, Tempora ...
- 【Java】Date类型获取年月日时分秒的两种方法(12小时制、24小时制)
Java的Date类型是,提供用来描述日期时间的类,它可以存储时间的年月日.时分秒的信息.但是如何从Date的实例中获取这些信息呢? 以前Date提供了一系列的get方法来获取,但是这些方法现在都被弃 ...
最新文章
- 一键生成CSDN文章的思维导图目录
- pd.merge 结果出现重复_COUNTIFS函数技巧之去重复值计数(思路分析,过程详解)...
- 高考查分数微信就能搞定
- SAP S/4HANA Customer Vendor Integration-CVI锁机制的实现
- 线段树(单点更新(模板)) 之 hdu 1166
- Linux whoami命令、Linux su命令、Linux w命令
- CV之LBP特征描述算子-人脸检测
- python画羊_羊车门作业 Python版
- 如何制作一款HTML5 RPG游戏引擎——第一篇,地图类的实现
- php 文件预览 水印,php实现在线预览word等office文件,同时添加水印
- CentOS搭建git服务器实测
- with cube 与with rollup 在group by子句中
- java将录音转换成文字,除了把录音转换成文字,原来录音转文字助手还有这么多实用功能...
- Oracle数据库锁表查询
- c语言 公式编辑器,AxMath(公式计算编辑器)
- Slate轨道工具使用(一)—Track,Clip
- 输入一个十进制整数,分别输出其二进制、八进制、十六进制字符串
- 汽车照明全国产化电子元件推荐方案
- Tecplot 输入数学符号
- Xftp的下载和安装(超详细)