今天遇到了这样一个场景:从数据库中取出的数据,日期信息(年月日)是以8位Long类型数字存放的(如20161116),时间信息(时分秒)是以6位Long类型数字存放的(如184253)。现需要将其转换为字符串,格式使用“yyyy-MM-dd HH:mm:ss”。
我写的工具类代码如下:
import java.text.MessageFormat;
public class DateTimeHelper {
/**
* 将8位数字转换为字符串yyyy-MM-dd
* @param date
* @return
*/
public static String getDateString(Long date) {
int year = 0;
int month = 1;
int day = 1;
if (date != null) {
int date_tmp = date.intValue();
int year_tmp = date_tmp / 10000;
int month_tmp = date_tmp % 10000 / 100;
int day_tmp = date_tmp % 100;
year = getValue(year_tmp, year, 0, 9999);
month = getValue(month_tmp, month, 1, 12);
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: {
day = getValue(day_tmp, day, 1, 31);
} break;
case 4:
case 6:
case 9:
case 11:{
day = getValue(day_tmp, day, 1, 30);
} break;
case 2: {
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
day = getValue(day_tmp, day, 1, 29);
} else {
day = getValue(day_tmp, day, 1, 28);
}
} break;
}
}
String result = MessageFormat.format("{0}-{1}-{2}",
String.format("%04d", year),
String.format("%02d", month),
String.format("%02d", day));
return result;
}
/**
* 将6位数字转换为字符串HH-mm-ss
* @param time
* @return
*/
public static String getTimeString(Long time) {
int hour = 0;
int minute = 0;
int second = 0;
if (time != null) {
int time_tmp = time.intValue();
int hour_tmp = time_tmp / 10000;
int minute_tmp = time_tmp % 10000 / 100;
int second_tmp = time_tmp % 100;
hour = getValue(hour_tmp, hour, 0, 23);
minute = getValue(minute_tmp, minute, 0, 59);
second = getValue(second_tmp, second, 0, 59);
}
String result = MessageFormat.format("{0}:{1}:{2}",
String.format("%02d", hour),
String.format("%02d", minute),
String.format("%02d", second));
return result;
}
/**
* 将8位数字转换为字符串yyyy-MM-dd 将6位数字转换为字符串HH-mm-ss
* @param date
* @param time
* @return
*/
public static String getDateTimeString(Long date, Long time) {
return getDateString(date) + " " + getTimeString(time);
}
/**
* 传入value值,如value在[min,max]内则返回value,否则返回默认值def
* @param value
* @param def
* @param min
* @param max
* @return
*/
private static int getValue(int value, int def, int min, int max) {
return (value >= min && value <= max) ? value : def;
}
}
内含三个函数:
1、public static String getDateString(Long date)
给定8位数字,如20161116L,返回yyyy-MM-dd格式字符串,如2016-11-16
2、public static String getTimeString(Long time)
给定6位数字,如184253L,返回HH:mm:ss格式字符串,如18:42:53
3、public static String getDateTimeString(Long date, Long time)
功能1和功能2的合集,返回yyyy-MM-dd HH:mm:ss格式字符串,如2016-11-16 18:42:53
其中,日期(年月日)的格式为{0}-{1}-{2},时间(时分秒)的格式为{0}:{1}:{2},可根据实际情况自行改动
测试调用代码如下:
public class JavaTest {
public static void main(String[] args) {
System.out.println("---getDateString---");
System.out.println(DateTimeHelper.getDateString(null));
System.out.println(DateTimeHelper.getDateString(20161116L));
System.out.println(DateTimeHelper.getDateString(99999999L));
System.out.println("---getTimeString---");
System.out.println(DateTimeHelper.getTimeString(null));
System.out.println(DateTimeHelper.getTimeString(160511L));
System.out.println(DateTimeHelper.getTimeString(999999L));
System.out.println("---getDateTimeString---");
System.out.println(DateTimeHelper.getDateTimeString(null, null));
System.out.println(DateTimeHelper.getDateTimeString(20161116L, null));
System.out.println(DateTimeHelper.getDateTimeString(null, 160511L));
System.out.println(DateTimeHelper.getDateTimeString(20161116L, 160511L));
System.out.println(DateTimeHelper.getDateTimeString(99999999L, 999999L));
}
}
程序运行结果如下:
---getDateString---
0000-01-01
2016-11-16
9999-01-01
---getTimeString---
00:00:00
16:05:11
00:00:00
---getDateTimeString---
0000-01-01 00:00:00
2016-11-16 00:00:00
0000-01-01 16:05:11
2016-11-16 16:05:11
9999-01-01 00:00:00
END
来源:oschina
链接:https://my.oschina.net/u/1425762/blog/789302