日期:2014-05-20  浏览次数:20844 次

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

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);
		}
	}

}



/**
 * 转换编码
 * @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];
		}
	}

}


 

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * <b>类名:DateTime.java</b> </br> 编写日期: 2006-6-23 <br/> 程序功能描述:日期时间的工具类 <br/>
 * Demo: <br/> Bug: <br/>
 * 
 * 程序变更日期 :<br/> 变更作者 :<br/> 变更说明 :<br/>
 * 
 * @author wuhua </br> <a href="mailto:rrq12345@163.com">rrq12345@163.com</a>
 */

public final class DateTime {
	private static String[] WEEKDAYS_EN = { "SUN", "MON", "TUE", "WED", "THU",
			"FRI", "SAT" };

	private static String[] WEEKDAYS_CH = { "周日", "周一", "周二", "周三", "周四", "周五",
			"周六" };

	public final String timeZone;

	public final int year;

	public final int month;

	public int day;

	public int weekday;

	public final int hour;

	public final int minute;

	public final int second;

	public final int millsecond;

	Calendar c;

	public DateTime(Date date, String timeZone) {
		this.timeZone = timeZone;
		c = timeZone == null ? Calendar.getInstance() : Calendar
				.getInstance(TimeZone.getDefault());
		c.setTime(date);
		year = c.get(Calendar.YEAR);
		month = c.get(Calendar.MONTH);
		day = c.get(Calendar.DAY_OF_MONTH);
		weekday = c.get(Calendar.DAY_OF_WEEK);
		hour = c.get(Calendar.HOUR_OF_DAY);
		minute = c.get(Calendar.MINUTE);
		second = c.get(Calendar.SECOND);
		millsecond = c.get(Calendar.MILLISECOND);
	}

	public DateTime(long time, String timeZone) {
		this(new Date(time), timeZone);
	}

	public DateTime() {
		this(System.currentTimeMillis(), "GMT + 16");
	}

	public static String beforeOneDate() {
		return new DateTime(System.currentTimeMillis() - 24 * 3600 * 1000,
				"GMT+8").toDateString();
	}

	public Date toDate() {
		Calendar c = Calendar.getInstance(TimeZone.getTimeZone(timeZone));
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month);
		c.set(Calendar.DAY_OF_MONTH, day);
		c.set(Calendar.HOUR_OF_DAY, hour);
		c.set(Calendar.MINUTE, minute);
		c.set(Calendar.SECOND, second);
		c.set(Calendar.MILLISECOND, millsecond);
		return c.getTime();
	}

	public String toDateString() {
		if (timeZone.equals("GMT-8"))
			return (month + 1) + "月" + day + "日" + " ["
					+ WEEKDAYS_EN[weekday - 1] + "] ";
		else
			return (month + 1) + "月" + day + "日" + " ["
					+ WEEKDAYS_CH[weekday - 1] + "] ";

	}

	public void setDate(int day) {

		this.day = day;
		this.c.set(Calendar.DAY_OF_WEEK, day);
		// day = c.get(Calendar.DAY_OF_WEEK);
		weekday = c.get(Calendar.DAY_OF_WEEK);

	}

	public String toTimeString() {
		return hour + ":" + minute + ":" + second + ":" + millsecond;
	}

	public String toString() {
		return toDateString() + " " + toTimeString();
	}
}