import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.text.DateFormat.Field;
import java.util.Date;java
public class StrUtils {
public static void main (String[] args) throws ParseException{
NumberFormat currency =NumberFormat.getCurrencyInstance();
NumberFormat integer = NumberFormat.getIntegerInstance();
NumberFormat number = NumberFormat.getNumberInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
System.out.println(currency.format(123.32));//¥123.32
System.out.println(currency.parse("¥234", new ParsePosition(0)));//234
System.out.println(currency.parse("3456", new ParsePosition(0)));//null
System.out.println(integer.format(123.63));//124
System.out.println(integer.isGroupingUsed());//true
System.out.println(integer.parse("388989",new ParsePosition(3)));//989
System.out.println(number.format(12.3347456));//12.335
System.out.println(number.format(34567.44545));//34,567.445
System.out.println(percent.format(3.34));//334%
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
DateFormat dateFormat = DateFormat.getDateInstance();
System.out.println(sdf.format(dateFormat.parse("2002-10-22")));//2002 10 22 00:00:00web
System.out.println(dateFormat.format(new Date(System.currentTimeMillis())));//2014-11-12數組
//這個方法暫時沒搞明白,FieldPosition 彷佛在這裏沒有起到做用...
System.out.println(dateFormat.format(sdf.parse("2010-10-22 10:21:32"),
new StringBuffer("2 "),new FieldPosition(Field.DAY_OF_YEAR,-1))); //2 2010-10-22
}測試
}spa
JAVA的各類變量中,開發用的最多的是字符串,字符串的相關操做比較重要。下面列舉兩種字符串翻轉方法:3d
經測試均正常:code
public class Reverse { /** * StringBuffer直接調用reverse方法 * @param str * @return */ public static String reverse1(String str){ if (str==null ){ return str; }else{ StringBuffer buffer = new StringBuffer(str); StringBuffer strs=buffer.reverse(); return strs.toString() ; } } /* * 藉助中間數組或者自主運算均可以 */ public static String reverse2(String str){ if (str==null){ return str; } char[] ch = str.toCharArray(); //char[] chs =new char[ch.length]; int mid=ch.length-1; for (int i=0;i<mid;i++){ //chs[i]=ch[ch.length-1-i]; char swap = ch[i]; ch[i] = ch[mid]; ch[mid--] = swap; } //return new String(chs); return new String(ch); } public static void main(String[] args) { System.out.println(reverse3("3464wxd我3dDRe")); } /* 還有一些遞歸或者其餘的方法... /* }