Object转为Stringçå ç§å½¢å¼
æ¬æå°å¯¹å¸¸ç¨ç转æ¢æ¹æ³è¿è¡ä¸ä¸ªæ»ç»ã常ç¨çæ¹æ³æObject.toString()ï¼(String)è¦è½¬æ¢ç对象ï¼String.valueOf(Object)çãä¸é¢å¯¹è¿äºæ¹æ³ä¸ä¸è¿è¡åæãæ¹æ³1ï¼éç¨ Object.toString()æ¹æ³ 请çä¸é¢çä¾åï¼Object object = getObject();System.out.println(object.toString());å¨è¿ç§ä½¿ç¨æ¹æ³ä¸ï¼å 为java.lang.Objectç±»éå·²æpublicæ¹æ³.toString()ï¼æ以对任ä½ä¸¥æ ¼æä¹ä¸çjava对象é½å¯ä»¥è°ç¨æ¤æ¹æ³ãä½å¨ä½¿ç¨æ¶è¦æ³¨æï¼å¿ é¡»ä¿è¯objectä¸æ¯nullå¼ï¼å¦åå°æåºNullPointerExceptionå¼å¸¸ãéç¨è¿ç§æ¹æ³æ¶ï¼é常派çç±»ä¼è¦çObjectéçtoString()æ¹æ³ãæ¹æ³2ï¼éç¨ç±»å转æ¢(String)objectæ¹æ³ è¿æ¯æ åçç±»å转æ¢ï¼å°object转æStringç±»åçå¼ã使ç¨è¿ç§æ¹æ³æ¶ï¼éè¦æ³¨æçæ¯ç±»åå¿ é¡»è½è½¬æStringç±»åãå æ¤æ好ç¨instanceofå个类åæ£æ¥ï¼ä»¥å¤ææ¯å¦å¯ä»¥è½¬æ¢ãå¦å容ææåºCalssCastExceptionå¼å¸¸ãæ¤å¤ï¼éç¹å«å°å¿çæ¯å å®ä¹ä¸ºObject ç±»åç对象å¨è½¬æStringæ¶è¯æ³æ£æ¥å¹¶ä¸ä¼æ¥éï¼è¿å°å¯è½å¯¼è´æ½å¨çé误åå¨ãè¿æ¶è¦æ ¼å¤å°å¿ãå¦ï¼Object obj = new Integer();String strVal = (String)obj;å¨è¿è¡æ¶å°ä¼åºéï¼å 为å°Integerç±»å强å¶è½¬æ¢ä¸ºStringç±»åï¼æ æ³éè¿ãä½æ¯ï¼Integer obj = new Integer();String strVal = (String)obj;å¦æ¯æ ¼å¼ä»£ç ï¼å°ä¼æ¥è¯æ³é误ãæ¤å¤ï¼å nullå¼å¯ä»¥å¼ºå¶è½¬æ¢ä¸ºä»»ä½java类类åï¼(String)nullä¹æ¯åæ³çãæ¹æ³3ï¼éç¨String.valueOf(Object) String.valueOf(Object)çåºç¡æ¯Object.toString()ãä½å®ä¸Object.toString()åææä¸åãå¨åé¢æ¹æ³1çåæä¸æå°ï¼ä½¿ç¨ç¬¬ä¸ç§æ¶éä¿è¯ä¸ä¸ºnullãä½éç¨ç¬¬ä¸ç§æ¹æ³æ¶ï¼å°ä¸ç¨æ å¿objectæ¯å¦ä¸ºnullå¼è¿ä¸é®é¢ã为äºä¾¿äºè¯´æé®é¢ï¼æ们æ¥åæä¸ä¸ç¸å ³çæºä»£ç ãJdkéString.valueOf(Object)æºç å¦ä¸ï¼/*** Returns the string representation of the Object argument.** @param obj an Object.* @return if the argument is 火火牛源码null, then a string equal to* "null"; otherwise, the value of* obj.toString() is returned.* @see java.lang.Object.toString()*/ public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString();}ä»ä¸é¢çæºç å¯ä»¥å¾æ¸ æ°ççåºnullå¼ä¸ç¨æ å¿ççç±ãä½æ¯ï¼è¿ä¹æ°æ°ç»äºæ们éæ£ãæ们åºå½æ³¨æå°ï¼å½object为nullæ¶ï¼String.valueOf(object)çå¼æ¯å符串"null"ï¼èä¸æ¯null!å¨ä½¿ç¨è¿ç¨ä¸åè®°è¦æ³¨æãè¯æ³ä¸ä¸ï¼å¦ææ们ç¨System.out.println(String.valueOf(null));System.out.println(null);æ们çå°çè¾åºå°æ¯ä¸æ¨¡ä¸æ ·çä¸è¥¿ï¼nullï¼ä½å®ä»¬æä¹ç¸ååï¼å¤æä¸ä¸ªå符串为空 s为ä¸ä¸ªå符串ï¼å¤æå®ä¸ºç©ºçæ¹æ³ï¼if (null==s ||"".equals(s)) { ...... } 注æï¼è¿éçnull==så"".equals(s)ä¸è¦åæs==nullås.equals(s)ï¼å 为""è¿ä¸ªå¼æ¯å·²ç»ç¡®å®çï¼é¢ç¥çï¼èsæ¯æªç¥çï¼æ以ç¨å¾ä¸å°å¿çæ¶ås.equals("")å°±ä¼åºç°nullpointå¼å¸¸ãå¨è¿éè½ç¶ä¸ä¼,å 为åé¢æif(null==s),ä½æ¯ä¹ æ¯è·å¨é£é使ç¨æ²¡æå ³ç³»çãä¸ä¸å®çequalsæ¹æ³ï¼å æ¬å ¶å®å¾å¤å¤çï¼å¦æç¨ç¡®å®çå¼å¤çé®é¢ä¼æ¯æªç¡®å®çå¤çå°å¾å¤bugã Stringç±»ååDateç±»åçç¸äºè½¬æ¢ å°String转æ¢ä¸ºDateï¼ String s="-- ::";java.text.SimpleDateFormat FormatDate = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); java.util.Date date = FormatDate.parse(s);//转æDateå°Date转æ¢ä¸ºString String.valueOf(date);Javaå¦ä½è·å¾ç³»ç»æ¶é´æè¿å¨å¦ä¹ Javaï¼ç»å¸¸å¨å·¥ä½ä¸éå°ä¸äºå°é®é¢ï¼Baiduä¹åï¼æ¾å°é®é¢ç解å³æ¹æ³ï¼è¦è®°å½ä¸æ¥å¦ï¼è¦ä¸ç¶ï¼åæçèåï¼è¯å®ä¸ä¹ å°±å¿è®°äºãåµåµãæ³è¦è·å¾ç³»ç»æ¶é´ï¼ä¸è¦æ¥æï¼åªè¦æ¶é´ï¼å¯æ¯Baiduåºæ¥çé½æ¯å¸¦çæ¥æåæ¶é´æ¡çï¼æ²¡æåç¬åªææ¶é´çä¾åãç¢ç£¨äºä¸ä¼ä¹åï¼æ¾å°é®é¢ç解å³æ¹æ³ã import java.util.Date;import java.text.DateFormat; Date now = new Date(); // Dateï¼ï¼æ¯java.util.Dateç±»çæé æ¹æ³ DateFormat d = DateFormat.getTimeInstance(); //getTimeInstance()è·å¾çæ¯ä¸å¸¦æ¥æçç³»ç»æ¶é´ String str = d.format(now); System.out.println("Today is " + str);//è¾åº 注ï¼1.Date now = new Date(); è¿å¥ä¸çDateï¼ï¼æ¯java.util.Dateç±»çæé æ¹æ³ï¼èä¸æ¯java.sqléçç±»ï¼æ以è¦å¨åé¢å ä¸import java.util.Date;èä¸æ¯ import java.sql.Date;è¿éæ¾ç»å 为ç¯äºè¿ç§ä½çº§é误è导è´ç¼è¯åºéã
2024-11-29 23:50
2024-11-29 23:38
2024-11-29 23:11
2024-11-29 22:40
2024-11-29 22:11