j2me常用的字符,日期,以及转换编码实现

  1. import java.io.UnsupportedEncodingException;   
  2. import java.util.Vector;   
  3.   
  4. import javax.wireless.messaging.Message;   
  5.   
  6. /**  
  7.  * 转换网络传过来的数据  
  8.  *   
  9.  * @auth colonel  
  10.  * @dateOrLeague 2006-7-11  
  11.  *   
  12.  */  
  13.   
  14. public class StringUtil {   
  15.         
  16.   
  17.     /**  
  18.      * 切割str字符串  
  19.      * 例如("wuhua,中国,好,",",");分割成String[] s = {"wuhua","中国","好");  
  20.      * @param str 源字符串  
  21.      * @param regex,分割标致,  
  22.      * @return  
  23.      */  
  24.     public static String[] split(String bufferstr, String regex) {   
  25.   
  26.         if(bufferstr == null)   
  27.             return null;   
  28.         Vector split = new Vector();   
  29.   
  30.         while (true// 处理从网络上获得的数据并对其进行处理   
  31.         {   
  32.             int index = bufferstr.indexOf(regex);   
  33.   
  34.             if (index == -1) {   
  35.                 if (bufferstr != null && !bufferstr.equals(""))   
  36.                     split.addElement(bufferstr);   
  37.                 // log.debug("bufferstr=" +bufferstr);s   
  38.                 break;   
  39.             }   
  40.             split.addElement(bufferstr.substring(0, index));   
  41.             // log.debug("Str=" + bufferstr.substring(0, index));   
  42.             bufferstr = bufferstr.substring(index + 1, bufferstr.length());   
  43.             // log.debug("bufferstr=" +bufferstr);   
  44.         }   
  45.         String[] s = new String[split.size()];   
  46.   
  47.         split.copyInto(s);   
  48.   
  49.         return s;   
  50.   
  51.     }   
  52.   
  53.        
  54.     /**  
  55.      * 转换网络上的字节为中文  
  56.      * @param bytes  
  57.      * @param start  
  58.      * @return  
  59.      */  
  60.     public static String getStringToGBK(byte[] bytes, int start) {   
  61.         byte[] rt = new byte[bytes.length - start];   
  62.         for (int i = 0; i < rt.length; i++)   
  63.             rt[i] = bytes[i + start];   
  64.         try {   
  65.             return new String(rt, "UTF-8");   
  66.         } catch (UnsupportedEncodingException e) {   
  67.             e.printStackTrace();   
  68.             return new String(rt);   
  69.         }   
  70.     }   
  71.   
  72. }  
import java.io.UnsupportedEncodingException;
import java.util.Vector;

import javax.wireless.messaging.Message;

/**
 * 转换网络传过来的数据
 * 
 * @auth colonel
 * @dateOrLeague 2006-7-11
 * 
 */

public class StringUtil {
	 

	/**
	 * 切割str字符串
	 * 例如("wuhua,中国,好,",",");分割成String[] s = {"wuhua","中国","好");
	 * @param str 源字符串
	 * @param regex,分割标致,
	 * @return
	 */
	public static String[] split(String bufferstr, String regex) {

		if(bufferstr == null)
			return null;
		Vector split = new Vector();

		while (true) // 处理从网络上获得的数据并对其进行处理
		{
			int index = bufferstr.indexOf(regex);

			if (index == -1) {
				if (bufferstr != null && !bufferstr.equals(""))
					split.addElement(bufferstr);
				// log.debug("bufferstr=" +bufferstr);s
				break;
			}
			split.addElement(bufferstr.substring(0, index));
			// log.debug("Str=" + bufferstr.substring(0, index));
			bufferstr = bufferstr.substring(index + 1, bufferstr.length());
			// log.debug("bufferstr=" +bufferstr);
		}
		String[] s = new String[split.size()];

		split.copyInto(s);

		return s;

	}

	
	/**
	 * 转换网络上的字节为中文
	 * @param bytes
	 * @param start
	 * @return
	 */
	public static String getStringToGBK(byte[] bytes, int start) {
		byte[] rt = new byte[bytes.length - start];
		for (int i = 0; i < rt.length; i++)
			rt[i] = bytes[i + start];
		try {
			return new String(rt, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return new String(rt);
		}
	}

}


Java代码 复制代码
  1.   
  2. /**  
  3.  * 转换编码  
  4.  * @author Administrator  
  5.  *  
  6.  */  
  7. public class FormatTransfer {   
  8.   
  9.     public static int getUINT4(byte ba[], int start) {   
  10.         int r = 0;   
  11.         r |= 0xff & ba[start];   
  12.         r <<= 8;   
  13.         r |= 0xff & ba[start + 1];   
  14.         r <<= 8;   
  15.         r |= 0xff & ba[start + 2];   
  16.         r <<= 8;   
  17.         r |= 0xff & ba[start + 3];   
  18.         return r;   
  19.     }   
  20.   
  21.     public static void setUINT4(byte ba[], int start, int value) {   
  22.         ba[start] = (byte) (value >> 24 & 0xff);   
  23.         ba[start + 1] = (byte) (value >> 16 & 0xff);   
  24.         ba[start + 2] = (byte) (value >> 8 & 0xff);   
  25.         ba[start + 3] = (byte) (value & 0xff);   
  26.     }   
  27.   
  28.     public static void setUSHORT4(byte ba[], int start, short value) {   
  29.         ba[start + 0] = (byte) (value >> 8 & 0xff);   
  30.         ba[start + 1] = (byte) (value & 0xff);   
  31.     }   
  32.   
  33.     public static short getUSHORT4(byte ba[], int start) {   
  34.         short r = 0;   
  35.         r |= 0xff & ba[start];   
  36.         r <<= 8;   
  37.         r |= 0xff & ba[start + 1];   
  38.         return r;   
  39.     }   
  40.   
  41.     public static void appen(byte[] rt, byte[] bodys, int start) {   
  42.         for (int i = 0; i < bodys.length; i++) {   
  43.             rt[start + i] = bodys[i];   
  44.         }   
  45.     }   
  46.   
  47. }  
/**
 * 转换编码
 * @author Administrator
 *
 */
public class FormatTransfer {

	public static int getUINT4(byte ba[], int start) {
		int r = 0;
		r |= 0xff & ba[start];
		r <<= 8;
		r |= 0xff & ba[start + 1];
		r <<= 8;
		r |= 0xff & ba[start + 2];
		r <<= 8;
		r |= 0xff & ba[start + 3];
		return r;
	}

	public static void setUINT4(byte ba[], int start, int value) {
		ba[start] = (byte) (value >> 24 & 0xff);
		ba[start + 1] = (byte) (value >> 16 & 0xff);
		ba[start + 2] = (byte) (value >> 8 & 0xff);
		ba[start + 3] = (byte) (value & 0xff);
	}

	public static void setUSHORT4(byte ba[], int start, short value) {
		ba[start + 0] = (byte) (value >> 8 & 0xff);
		ba[start + 1] = (byte) (value & 0xff);
	}

	public static short getUSHORT4(byte ba[], int start) {
		short r = 0;
		r |= 0xff & ba[start];
		r <<= 8;
		r |= 0xff & ba[start + 1];
		return r;
	}

	public static void appen(byte[] rt, byte[] bodys, int start) {
		for (int i = 0; i < bodys.length; i++) {
			rt[start + i] = bodys[i];
		}
	}

}


Java代码 复制代码
  1.     
  2.   
  3. import java.util.Calendar;   
  4. import java.util.Date;   
  5. import java.util.TimeZone;   
  6.   
  7. /**  
  8.  * <b>类名:DateTime.java</b> </br> 编写日期: 2006-6-23 <br/> 程序功能描述:日期时间的工具类 <br/>  
  9.  * Demo: <br/> Bug: <br/>  
  10.  *   
  11.  * 程序变更日期 :<br/> 变更作者 :<br/> 变更说明 :<br/>  
  12.  *   
  13.  * @author wuhua </br> <a href="mailto:rrq12345@163.com">rrq12345@163.com</a>  
  14.  */  
  15.   
  16. public final class DateTime {   
  17.     private static String[] WEEKDAYS_EN = { "SUN""MON""TUE""WED""THU",   
  18.             "FRI""SAT" };   
  19.   
  20.     private static String[] WEEKDAYS_CH = { "周日""周一""周二""周三""周四""周五",   
  21.             "周六" };   
  22.   
  23.     public final String timeZone;   
  24.   
  25.     public final int year;   
  26.   
  27.     public final int month;   
  28.   
  29.     public int day;   
  30.   
  31.     public int weekday;   
  32.   
  33.     public final int hour;   
  34.   
  35.     public final int minute;   
  36.   
  37.     public final int second;   
  38.   
  39.     public final int millsecond;   
  40.   
  41.     Calendar c;   
  42.   
  43.     public DateTime(Date date, String timeZone) {   
  44.         this.timeZone = timeZone;   
  45.         c = timeZone == null ? Calendar.getInstance() : Calendar   
  46.                 .getInstance(TimeZone.getDefault());   
  47.         c.setTime(date);   
  48.         year = c.get(Calendar.YEAR);   
  49.         month = c.get(Calendar.MONTH);   
  50.         day = c.get(Calendar.DAY_OF_MONTH);   
  51.         weekday = c.get(Calendar.DAY_OF_WEEK);   
  52.         hour = c.get(Calendar.HOUR_OF_DAY);   
  53.         minute = c.get(Calendar.MINUTE);   
  54.         second = c.get(Calendar.SECOND);   
  55.         millsecond = c.get(Calendar.MILLISECOND);   
  56.     }   
  57.   
  58.     public DateTime(long time, String timeZone) {   
  59.         this(new Date(time), timeZone);   
  60.     }   
  61.   
  62.     public DateTime() {   
  63.         this(System.currentTimeMillis(), "GMT + 16");   
  64.     }   
  65.   
  66.     public static String beforeOneDate() {   
  67.         return new DateTime(System.currentTimeMillis() - 24 * 3600 * 1000,   
  68.                 "GMT+8").toDateString();   
  69.     }   
  70.   
  71.     public Date toDate() {   
  72.         Calendar c = Calendar.getInstance(TimeZone.getTimeZone(timeZone));   
  73.         c.set(Calendar.YEAR, year);   
  74.         c.set(Calendar.MONTH, month);   
  75.         c.set(Calendar.DAY_OF_MONTH, day);   
  76.         c.set(Calendar.HOUR_OF_DAY, hour);   
  77.         c.set(Calendar.MINUTE, minute);   
  78.         c.set(Calendar.SECOND, second);   
  79.         c.set(Calendar.MILLISECOND, millsecond);   
  80.         return c.getTime();   
  81.     }   
  82.   
  83.     public String toDateString() {   
  84.         if (timeZone.equals("GMT-8"))   
  85.             return (month + 1) + "月" + day + "日" + " ["  
  86.                     + WEEKDAYS_EN[weekday - 1] + "] ";   
  87.         else  
  88.             return (month + 1) + "月" + day + "日" + " ["  
  89.                     + WEEKDAYS_CH[weekday - 1] + "] ";   
  90.   
  91.     }   
  92.   
  93.     public void setDate(int day) {   
  94.   
  95.         this.day = day;   
  96.         this.c.set(Calendar.DAY_OF_WEEK, day);   
  97.         // day = c.get(Calendar.DAY_OF_WEEK);   
  98.         weekday = c.get(Calendar.DAY_OF_WEEK);   
  99.   
  100.     }   
  101.   
  102.     public String toTimeString() {   
  103.         return hour + ":" + minute + ":" + second + ":" + millsecond;   
  104.     }   
  105.   
  106.     public String toString() {   
  107.         return toDateString() + " " + toTimeString();   
  108.     }   
  109. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值