J2ME时间例子

下面是一个时间例子:

 

Calendar.getInstance(TimeZone.getTimeZone("GMT+8:00"));

 

可以解决J2ME慢八小时的问题,它是根据时区来设置的!

 

package com.iwidsets.util;

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

public class TimeUtil {

	private static Calendar calendar;

	private static StringBuffer timeBuffer;

	private static Date date;
	static {
		calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8:00"));
		date = new Date();
	}

	private static void setTime(long time) {
		date.setTime(time);
		calendar.setTime(date);
	}

	/**
	 * Put "0" to the beginning of time string if it is less than 10.
	 */
	public static String formatTimeInteger(int n) {
		if (n < 10)
			return "0" + n;
		else
			return String.valueOf(n);
	}

	/**
	 * Put "0" to the beginning of time string if it is less than 1000.
	 */
	private static String formatYearInteger(int n) {
		if (n < 10)
			return "000" + n;
		else if (n < 100)
			return "00" + n;
		else if (n < 1000)
			return "0" + n;
		else
			return String.valueOf(n);
	}

	/**
	 * Convert time to string. YYYY-MM-DD HH:MM:SS
	 */
	public static synchronized String toString(long time) {
		return toString(time, true, true);
	}

	public static synchronized String toString(long time,
			boolean hasDelimitors, boolean hasTime) {
		setTime(time);
		StringBuffer s = new StringBuffer();
		s.append(formatYearInteger(calendar.get(Calendar.YEAR))).append(
				hasDelimitors ? "-" : "");
		s.append(formatTimeInteger(calendar.get(Calendar.MONTH) + 1)).append(
				hasDelimitors ? "-" : "");
		s.append(formatTimeInteger(calendar.get(Calendar.DAY_OF_MONTH)))
				.append(hasDelimitors && hasTime ? " " : "");
		if (hasTime) {
			s.append(formatTimeInteger(calendar.get(Calendar.HOUR_OF_DAY)))
					.append(hasDelimitors ? ":" : "");
			s.append(formatTimeInteger(calendar.get(Calendar.MINUTE))).append(
					hasDelimitors ? ":" : "");
			s.append(formatTimeInteger(calendar.get(Calendar.SECOND)));
		}
		return s.toString();
	}

	public static synchronized String getHM(long time) {
		setTime(time);
		timeBuffer = new StringBuffer();
		timeBuffer
				.append(formatTimeInteger(calendar.get(Calendar.HOUR_OF_DAY)))
				.append(":");
		timeBuffer.append(formatTimeInteger(calendar.get(Calendar.MINUTE)));
		return timeBuffer.toString();
	}
}

 

JAVA例子:

	public static String getHM() {
		SimpleDateFormat bartDateFormat = new SimpleDateFormat("HH:mm");
		Date date = new Date();
		return bartDateFormat.format(date);
	}

 

HH:mm 为24小时制

hh:mm 为12小时制

 

MP3Dict应用发布了

一些很特别的J2ME开源项目

基于J2ME平台的Log4j

推荐一个android学习网站

java

你可能感兴趣的:(log4j,android)