程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> j2me常用的字符,日期,以及轉換編碼實現代碼

j2me常用的字符,日期,以及轉換編碼實現代碼

編輯: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:[email protected]">[email protected]</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();   
    }   
}  

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved