JAVA如何parse出一段字符串里的连续8个数字 java怎么把字符串转换成日期类型
JAVA String\u8f6cfloat\u540e\u503c\u4e0d\u5bf9\uff0c\u5b57\u7b26\u4e32134886.07\u901a\u8fc7Float.parseFloat(134886.07)\u8f6c\u6362\u540e\u7684\u503c\u4e3a134886.06float\u6709\u7cbe\u5ea6\u9650\u5236\uff0c\u5e76\u4e14float\u53ea\u80fd\u8868\u793a7\u4f4d\u6709\u6548\u6570\u5b57\uff0c134886.07\u5df2\u7ecf\u8d85\u51fa\u4e86float\u7684\u7cbe\u5ea6\u8303\u56f4\uff0c\u5efa\u8bae\u4f7f\u7528double
SimpleDateFormat\u7c7b
\u6709\u4e2aparse\u65b9\u6cd5
public Date parse(String text, ParsePosition pos)
\u89e3\u6790\u5b57\u7b26\u4e32\u7684\u6587\u672c\uff0c\u751f\u6210 Date\u3002
\u6b64\u65b9\u6cd5\u8bd5\u56fe\u89e3\u6790\u4ece pos \u7ed9\u5b9a\u7684\u7d22\u5f15\u5904\u5f00\u59cb\u7684\u6587\u672c\u3002\u5982\u679c\u89e3\u6790\u6210\u529f\uff0c\u5219\u5c06 pos \u7684\u7d22\u5f15\u66f4\u65b0\u4e3a\u6240\u7528\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u540e\u9762\u7684\u7d22\u5f15\uff08\u4e0d\u5fc5\u5bf9\u76f4\u5230\u5b57\u7b26\u4e32\u7ed3\u5c3e\u7684\u6240\u6709\u5b57\u7b26\u8fdb\u884c\u89e3\u6790\uff09\uff0c\u5e76\u8fd4\u56de\u89e3\u6790\u5f97\u5230\u7684\u65e5\u671f\u3002\u66f4\u65b0\u540e\u7684 pos \u53ef\u4ee5\u7528\u6765\u6307\u793a\u4e0b\u6b21\u8c03\u7528\u6b64\u65b9\u6cd5\u7684\u8d77\u59cb\u70b9\u3002\u5982\u679c\u53d1\u751f\u9519\u8bef\uff0c\u5219\u4e0d\u66f4\u6539 pos \u7684\u7d22\u5f15\uff0c\u5e76\u5c06 pos \u7684\u9519\u8bef\u7d22\u5f15\u8bbe\u7f6e\u4e3a\u53d1\u751f\u9519\u8bef\u5904\u7684\u5b57\u7b26\u7d22\u5f15\uff0c\u5e76\u4e14\u8fd4\u56de null\u3002
\u6307\u5b9a\u8005\uff1a
\u7c7b DateFormat \u4e2d\u7684 parse
\u53c2\u6570\uff1a
text - \u5e94\u8be5\u89e3\u6790\u5176\u4e2d\u4e00\u90e8\u5206\u7684 String\u3002
pos - \u5177\u6709\u4ee5\u4e0a\u6240\u8ff0\u7684\u7d22\u5f15\u548c\u9519\u8bef\u7d22\u5f15\u4fe1\u606f\u7684 ParsePosition \u5bf9\u8c61\u3002
\u8fd4\u56de\uff1a
\u4ece\u5b57\u7b26\u4e32\u8fdb\u884c\u89e3\u6790\u7684 Date\u3002\u5982\u679c\u53d1\u751f\u9519\u8bef\uff0c\u5219\u8fd4\u56de null\u3002
\u629b\u51fa\uff1a
NullPointerException - \u5982\u679c text \u6216 pos \u4e3a null\u3002
\u53e6\u8bf7\u53c2\u89c1\uff1a
DateFormat.setLenient(boolean)
2 Pattern p = Pattern.compile("\\d{8}");
3 Matcher m = p.matcher(str);
4 String result;
5 return result = m.find() ? m.group():null;
6 }
java中String字符串转化为数字:
转换为浮点型:
使用Double或者Float的parseDouble或者parseFloat方法进行转换
String s = "123.456 "; //要确保字符串为一个数值,否则会出异常
double d = Double.parseDouble(s);
float f = Float.parseFloat(s);
转换为整型:
使用Integer的parseInt方法进行转换。
int i = Integer.parseInt([String]);//[String]待转换的字符串
public class Test {
public static void main(String[] args) {
String str = "123321";
int num = Integer.parseInt(str);
System.out.println(num);
}
}
必须要保证str字符串都是数字才行,如果不是数字,会报NumberFormatException异常
private static String func(String str) {
Pattern p = Pattern.compile("\\d{8}");
Matcher m = p.matcher(str);
String result;
return result = m.find() ? m.group():null;
}
返回字符串中的第一个8位数字, 注意是string类型, 需要就转成int
public class Util {
public static void main(String[] args) {
String str = "abcdkjer?id12345678da";
String result = "";
for(int i = 0; i < str.length(); i++){
try{
result += Integer.parseInt(str.substring(i, (i+1)));
}catch(Exception e){
}
}
System.out.println(result);
}
}
String regex = "[0-9]{8}"
正则表达 应该是这样
绛旓細寤鸿閲囩敤jsoup鏉ユ姄鍙栧拰瑙f瀽鏂囦欢銆俲soup鏀寔css閫夋嫨鍣ㄣ備笅闈㈡槸涓涓畝鍗曠殑渚嬪瓙锛欴ocument doc = Jsoup.parse(in/*鏂囦欢娴*/, charset/*瀛楃闆*/, service/*鐩稿鍦板潃*/);Element e = doc.select("div[id=myid]").get(0);// 鐢ㄩ夋嫨鍣ㄩ夋嫨鍏冪礌List<Node> childs = new LinkedList<Node>(e....
绛旓細import java.text.SimpleDateFormat;import java.util.Date;public class DateTest { public static void main(String[] args) throws ParseException{ String date="1988-10-10 20:10:10";Date d=null;SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");d=sdf.parse(date);/...
绛旓細Date date1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy ").parse("Sat Dec 01 00:30:10 CST 2012");String out = new SimpleDateFormat("y-M-d dd:HH:ss").format(date1);濡傛灉涓婇潰鐨勬牸寮忎笉瀵圭収涓嬮潰鐨勮〃鏀规敼鍚 G Era 鏍囧織绗 Text AD y 骞 Year 1996; 96 M...
绛旓細//java璨屼技浠庢帶鍒跺彴璇诲叆鐨勯兘鏄瓧绗︿覆锛屼竴鑸兘瑕佽繘琛岃浆鍖栥係ystem.out.println(s);System.out.println("璇疯緭鍏ヤ竴涓瓧绗︼細");char c = br.readLine().charAt(0);System.out.println(c);System.out.println("璇疯緭鍏ヤ竴涓弻绮惧害瀹炴暟锛");double d = Double.parseDouble(br.readLine()); //杞寲...
绛旓細public static void main(String[] args){ String a = "a1v2.3s5";char[] b = a.toCharArray();String result = "";for (int i = 0; i < b.length; i++){ if (("0123456789.").indexOf(b[i] + "") != -1){ result += b[i];} } System.out.println(result);} 鎶...
绛旓細棣栧厛瑕佹湁涓緭鍏ユ祦InputStreamReader ir;鍐嶆湁涓紦瀛楤ufferedReader in锛涚劧鍚 ir = new InputStreamReader(System.in);in = new BufferedReader(ir);杩欐槸杈撳叆 涔嬪悗in.ReaderLine();鎸夎璇诲彇锛屼緥濡傦細class IOTest1 { public static void main(String args[]){ String str;//鍒涘缓涓涓狟ufferedReader瀵硅薄...
绛旓細Date绫诲瀷灏辨槸杩欑鏍煎紡鐨勩備綘濡傛灉鎯崇敤Date 杩樻槸涓嶆槸杩欑鏍煎紡鐨勩傛槸涓嶅彲鑳界殑銆
绛旓細JAVA int float long涓嶴tring浜掔浉杞崲鐨勬柟娉 1 濡備綍灏嗗瓧涓 String 杞崲鎴愭暣鏁 int?A. 鏈変袱涓柟娉:1).int i = Integer.parseInt([String]);i = Integer.parseInt([String],[int radix]);2). int i = Integer.valueOf(my_str).intValue();娉: 瀛椾覆杞垚 Double, Float, Long 鐨勬柟娉曞ぇ鍚...
绛旓細缁欎綘璇翠笅鎬濊矾 浣犺緭鍏ョ殑琛ㄨ揪寮忔槸涓涓 瀛楃涓瞺 鈥1+2*3+3/4+2*3-5鈥濈敤java鐨姝e垯琛ㄨ揪寮忚В鏋愯琛ㄨ揪寮 鎶婃暟瀛楀拰杩愮畻绗︿緷娆¤鍙鍑烘潵 鐒跺悗杞崲涓簀ava琛ㄨ揪寮 1+2*3+3/4+2*3-5 鐩存帴杈撳嚭灏卞彲浠ヤ簡~
绛旓細Double绫讳腑鏈潵灏辨湁涓柟娉parseDouble() 锛屼綔鐢ㄦ槸鎶婂瓧绗︿覆杞崲涓篋ouble銆傛瘮濡備綘瑕佹妸瀛楃涓茶浆鎹负鏁存暟涔熸槸涓鏍风殑鏂规硶:int a=Integer.parseInt("");鑻辨枃鎰忔濇槸锛氳В鏋愶紝鍏跺疄灏辨槸鎶婂叾浠栨暟鎹紙ch锛夌粰鎴戣浆鎹㈡垚 Double鐨勬剰鎬