2014年5月26日星期一

String into Date type of problem

previously Date date = new Date (Str s) s can be converted to Date type.
now also be specified with Simpledateformat style : If Simpledateformat ss = new Simpledateformat ("yyyy-MM-dd HH: mm: ss") Date type to turn through the parse.
But there is a problem, I wanted to write a utility class , I do not know what kind of style the incoming string , how to convert Date types? Seek expert advice ah
------ Solution ------------------------------------ --------
not. . .
------ Solution ---------------------------------------- ----
fully automatic is not possible, the regular expression can be specified in several formats that can be entered and then determine the conversion
------ Solution ------------ --------------------------------
exhaustive of all types , according to the passed parameter types to match to type conversion .
------ Solution ---------------------------------------- ----
pass two parameters can write a utility class , such as:

public static Date toDateSafely(String simpleType,String stringDate){
try{
SimpleDateFormat dateFormat = new SimpleDateFormat(simpleType); 
    return dateFormat.parse(stringDate); 
}catch(Exception e){
return null;
}
}


------ Solution ------------------------------------- -------
commons there DateUtils can
------ Solution ------------------------ --------------------
make a sauce just 22
------ Solution ------------ --------------------------------

/**
 * String 转 Date
 * auther: shijing
 * 2014-5-9下午03:16:14
 * @param str
 * @param format
 * @return
 */
public static Date string2Date(String str, String format) {
DateFormat dateFormat = new SimpleDateFormat(format);
Date date = null;
try {
date = dateFormat.parse(str);
}
catch (ParseException e) {
e.printStackTrace();
}
return date;
}


This is what I often write DateUtils One way
------ Solution ------------------------ --------------------
uncertainty inflicted on one parameter . . .
------ Solution ---------------------------------------- ----
as exhaustive . . .
constantly try catch to try and succeed on the return, failure to continue to parse.
------ Solution ---------------------------------------- ----

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test3 {
public static Date parseDate(String time) {
String pattern = "";
if (time.contains("-")) {
pattern += "yyyy-MM-dd";
} else if (time.contains("年")) {
pattern += "yyyy年MM月dd日";
}
if (time.contains("上午") || time.contains("下午")) {
if (!pattern.equals("")) {
pattern += " ";
}
pattern += "a";
}
if (time.contains(":")) {
if (!pattern.equals("")) {
pattern += " ";
}
pattern += "HH:mm:ss";
}
try {
return new SimpleDateFormat(pattern).parse(time);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}

public static void main(String[] args) {
System.out.println(parseDate("2014-05-26 下午 16:22:13"));
System.out.println(parseDate("2014-05-26"));
System.out.println(parseDate("16:22:13"));
System.out.println(parseDate("2014-05-26 16:22:13"));
System.out.println(parseDate("2014年05月26日 16:22:13"));
}
}

basic common no problem
------ Solution ------------------------------- -------------
lazy to have boundaries. . Unless you know your format is fixed and that several, and then one by one to try , otherwise it is impossible to have a universal method 01/02/2014
For example , the format does not tell you , how do you know this is February 1 , or January 2 ? Both formats are very common format , so I can not judge
------ For reference only ------------------------ ---------------
For example, I now have two strings
		String input1="2014-05-23 09:23:33";
String input2="2014-05-23 上午09:23:33";
how to put them into a Date type ,
with SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); input1 can only turn one way how to write as much as possible . match many styles , or what can be achieved?
------ For reference only -------------------------------------- -
able to write a simple example ?
------ For reference only ---------------------------------------
en, yes, but every time you call this method must know what form stringDate time string to the corresponding rid simpleType. lazy I think the point is , I do not have to care about what it was like passed in stringDate , give me time to turn on the type of good
------ For reference only ------------------------------------- -
Thank you join , knot posted . No wonder, then ah unequal share

没有评论:

发表评论