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; 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:00 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 }}
JAVA的各种变量中,开发用的最多的是字符串,字符串的相关操作比较重要。下面列举两种字符串翻转方法:
经测试均正常:
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