java正则匹配解析出时间以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
java正则匹配解析出时间
package zhengze;
import java text ParseException;
import java text SimpleDateFormat;
import java util regex *;
import java util Date;
public class zhengze {
public static void main(String[] args) throws ParseException {
// String path =
// /user/sunflower/flume/msp/ / / /vortex pro hadoop ;
// String reg =
// ^\\d{ } [ ] [ ] [ ]|[ ]\\d| [ ] [ ][ ]:[ ]\\d:[ ]\\d ;
// String checkValue = / ;
// String reg = ^\\d{ } [ ]\\d [ ]\\d/[ ][ ]$ ;
//
// String reg =
// ^\\d{ } [ ][ ] [ ][ ] [ ][ ]:[ ]\\d:[ ]\\d$ ;
//
// Pattern p = pile(reg)
// Matcher m = p matcher(checkValue)
// boolean b = m matches()
// if(b)
// {
//
// System out println( 格式正确 )
// }
// else
// {
// System out println( 格式错误 )
// }
String path = /string/string/string/ / /string/string ;
// Pattern pattern = pile( \\d{ } [ ]\\d [ ]\\d/[ ]\\d )
// Matcher matcher = pattern matcher(path)
//
// if (matcher find()) {
// String src = matcher group()
// System out println(src)
// SimpleDateFormat sdf = new SimpleDateFormat( yyyy MM dd/hh )
// Date date = sdf parse(src)
// System out println(date)
//
// long t = date getTime() + * ;
//
// System out println(t)
//
// }
Pattern pattern = pile( \\d{ } [ ]\\d [ ]\\d/[ ]\\d )
Matcher matcher = pattern matcher(path)
Date datefile = null;
if (matcher find()) {
String src = matcher group()
System out println(src)
SimpleDateFormat df = new SimpleDateFormat( yyyy MM dd/hh )
try {
datefile = df parse(src)
} catch (ParseException e) {
// TODO Auto generated catch block
e printStackTrace()
}
}
long readyTime = datefile getTime() + * ;
System out println(datefile)
System out println(datefile getTime())
System out println(readyTime)
Date d = new Date(readyTime)
System out println(d toString())
} lishixinzhi/Article/program/Java/hx/201311/26307
分页:123