python string转date类型_java string类型怎么转化成date类型

/**

* 根据一段日期时间字符串,转换得到日期时间对象。日期时间字符串可以是以下格式:

* 1. yyyy-MM

* 2. yyyy-MM-dd

* 3. HH:mm

* 4. HH:mm:ss

* 5. yyyy-MM-dd HH:mm

* 6. yyyy-MM-dd HH:mm:ss

*

* @param str

* 日期时间字符串

* @return 日期时间对象

*/

public static Date parseDate(String str) {

// 没有输入值

if (str == null)

return null;

str = str.trim();

if (str.length() == 0)

return null;

str = str.replace('/', '-');

str = str.replace('\\', '-');

// yyyy-MM-dd HH:mm:ss

String[] sa = str.split("\\s+");

if (sa == null || sa.length == 0)

return null;

// 年月日

String s1 = null;

// 时分秒

String s2 = null;

if (sa.length == 1) {

// 可能是 年月日,也可能是 时分秒

if (sa[0].indexOf(":") > 0) {

s2 = sa[0];

} else {

s1 = sa[0];

}

}

if (sa.length >= 2) {

for (int i = 0; sa != null && i < 2 && i < sa.length; i++) {

if (sa[i].indexOf(":") > 0 || s1 != null) {

s2 = sa[i];

} else {

s1 = sa[i];

}

}

}

if (s1 == null)

s1 = "1900-01-01";

if (s2 == null)

s2 = "00:00:00";

int[] ymd = getYmd(s1);

int[] hms = getHms(s2);

Calendar cal = Calendar.getInstance();

cal.set(ymd[0], ymd[1] - 1, ymd[2], hms[0], hms[1], hms[2]);

return cal.getTime();

}

private static int[] getHms(String s2) {

int[] hms = { 0, 0, 0 };

int nEnd = s2.indexOf(":");

if (nEnd < 0) {

// 只有小时

// 第1个数字

hms[0] = LogicUtility.parseInt(s2, hms[0]);

return hms;

}

int nStart = 0;

// 时分秒

// 第1个数字

hms[0] = LogicUtility.parseInt(s2.substring(nStart, nEnd), hms[0]);

// 第2个数字

nStart = nEnd + 1;

if (nStart >= s2.length())

return hms;

nEnd = s2.indexOf(":", nStart);

if (nEnd < 0) {

// 只有1个 -

hms[1] = LogicUtility.parseInt(s2.substring(nStart), hms[1]);

nEnd = s2.length();

} else {

hms[1] = LogicUtility.parseInt(s2.substring(nStart, nEnd), hms[1]);

}

// 第3个数字

nStart = nEnd + 1;

if (nStart >= s2.length())

return hms;

hms[2] = LogicUtility.parseInt(s2.substring(nStart), hms[2]);

return hms;

}

private static int[] getYmd(String s1) {

int[] ymd = { 1900, 1, 1 };

int nEnd = s1.indexOf("-");

if (nEnd < 0) {

// 只有小时

// 第1个数字

ymd[0] = LogicUtility.parseInt(s1, ymd[0]);

return ymd;

}

int nStart = 0;

// 年月日

// 第1个数字

ymd[0] = LogicUtility.parseInt(s1.substring(nStart, nEnd), ymd[0]);

// 第2个数字

nStart = nEnd + 1;

if (nStart >= s1.length())

return ymd;

nEnd = s1.indexOf("-", nStart);

if (nEnd < 0) {

// 只有1个 -

ymd[1] = LogicUtility.parseInt(s1.substring(nStart), ymd[1]);

nEnd = s1.length();

} else {

ymd[1] = LogicUtility.parseInt(s1.substring(nStart, nEnd), ymd[1]);

}

// 第3个数字

nStart = nEnd + 1;

if (nStart >= s1.length())

return ymd;

ymd[2] = LogicUtility.parseInt(s1.substring(nStart), ymd[2]);

return ymd;

}

其中LogicUtility.parseInt的方法如下:

/**

* 得到传入的字符串字面表示的十进制整型数据,如果该字符串字面表示的不是数字,

* 则取用默认整型数值

*

* @param strValue

* 字符串

* @param nDefaultValue

* 默认的整型数值

* @return 字面表示的十进制整型数据

*/

public static int parseInt(String strValue, int nDefaultValue) {

// parse

return (int) parseDouble(strValue, (double) nDefaultValue);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值