package com.hoten.util;
import java.util.*;
import java.io.*;
/**
* <p>Title: Time </p>
* <p>Description: </p>
* 此类主要用来取得本地系统的系统时间并用下面5种格式显示
* 1. YYMMDDHH 8位
* 2. YYMMDDHHmm 10位
* 3. YYMMDDHHmmss 12位
* 4. YYYYMMDDHHmmss 14位
* 5. YYMMDDHHmmssxxx 15位 (最后的xxx 是毫秒)
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: c-platform</p>
* @author WuJiaQian
* @version 1.0
*/
public class CTime {
public static final int YYMMDDhhmmssxxx = 15;
public static final int YYYYMMDDhhmmss = 14;
public static final int YYMMDDhhmmss = 12;
public static final int YYMMDDhhmm = 10;
public static final int YYMMDDhh = 8;
/**
* 取得本地系统的时间,时间格式由参数决定
* @param format 时间格式由常量决定
* @return String 具有format格式的字符串
*/
public static String getTime(int format) {
StringBuffer cTime = new StringBuffer(15);
Calendar time = Calendar.getInstance();
int miltime = time.get(Calendar.MILLISECOND);
int second = time.get(Calendar.SECOND);
int minute = time.get(Calendar.MINUTE);
int hour = time.get(Calendar.HOUR_OF_DAY);
int day = time.get(Calendar.DAY_OF_MONTH);
int month = time.get(Calendar.MONTH) + 1;
int year = time.get(Calendar.YEAR);
time = null;
if (format != 14) {
if (year >= 2000) year = year - 2000;
else year = year - 1900;
}
if (format >= 2) {
if (format == 14) cTime.append(year);
else cTime.append(getFormatTime(year, 2));
}
if (format >= 4)
cTime.append(getFormatTime(month, 2));
if (format >= 6)
cTime.append(getFormatTime(day, 2));
if (format >= 8)
cTime.append(getFormatTime(hour, 2));
if (format >= 10)
cTime.append(getFormatTime(minute, 2));
if (format >= 12)
cTime.append(getFormatTime(second, 2));
if (format >= 15)
cTime.append(getFormatTime(miltime, 3));
return cTime.toString().trim();
}
/**
* 产生任意位的字符串
* @param time int 要转换格式的时间
* @param format int 转换的格式
* @return String 转换的时间
*/
public synchronized static String getYearAdd(int format, int iyear) {
StringBuffer cTime = new StringBuffer(10);
Calendar time = Calendar.getInstance();
time.add(Calendar.YEAR, iyear);
int miltime = time.get(Calendar.MILLISECOND);
int second = time.get(Calendar.SECOND);
int minute = time.get(Calendar.MINUTE);
int hour = time.get(Calendar.HOUR_OF_DAY);
int day = time.get(Calendar.DAY_OF_MONTH);
int month = time.get(Calendar.MONTH) + 1;
int year = time.get(Calendar.YEAR);
if (format != 14) {
if (year >= 2000) year = year - 2000;
else year = year - 1900;
}
if (format >= 2) {
if (format == 14) cTime.append(year);
else cTime.append(getFormatTime(year, 2));
}
if (format >= 4)
cTime.append(getFormatTime(month, 2));
if (format >= 6)
cTime.append(getFormatTime(day, 2));
if (format >= 8)
cTime.append(getFormatTime(hour, 2));
if (format >= 10)
cTime.append(getFormatTime(minute, 2));
if (format >= 12)
cTime.append(getFormatTime(second, 2));
if (format >= 15)
cTime.append(getFormatTime(miltime, 3));
return cTime.toString();
}
/**
* 产生任意位的字符串
* @param time int 要转换格式的时间
* @param format int 转换的格式
* @return String 转换的时间
*/
private static String getFormatTime(int time, int format) {
StringBuffer numm = new StringBuffer(format);
int length = String.valueOf(time).length();
if (format < length)return null;
for (int i = 0; i < format - length; i++) {
numm.append("0");
}
numm.append(time);
return numm.toString().trim();
}
/**
* 本函数主要作用是返回当前年份
* @param len int 要转换年的位数
* @return String 处理后的年
*/
public static String getYear(int len) {
Calendar time = Calendar.getInstance();
int year = time.get(Calendar.YEAR);
String djyear = Integer.toString(year);
if (len == 2) {
djyear = djyear.substring(2);
}
return djyear;
}
/*
#本函数作用是返回当前月份(2位)
*/
public static String getMonth() {
Calendar time = Calendar.getInstance();
int month = time.get(Calendar.MONTH) + 1;
String djmonth = "";
if (month < 10) {
djmonth = "0" + Integer.toString(month);
}
else {
djmonth = Integer.toString(month);
}
return djmonth;
}
/*
#本函数作用是返回上个月份(2位)
*/
public static String getPreMonth() {
Calendar time = Calendar.getInstance();
int month = time.get(Calendar.MONTH);
if (month == 0) month = 12;
String djmonth = "";
if (month < 10) {
djmonth = "0" + Integer.toString(month);
}
else {
djmonth = Integer.toString(month);
}
return djmonth;
}
/*
#本函数主要作用是返回当前天数
*/
public static String getDay() {
Calendar time = Calendar.getInstance();
int day = time.get(Calendar.DAY_OF_MONTH);
String djday = "";
if (day < 10) {
djday = "0" + Integer.toString(day);
}
else {
djday = Integer.toString(day);
}
return djday;
}
/*
本函数作用是返回当前小时
*/
public static String getHour() {
Calendar time = Calendar.getInstance();
int hour = time.get(Calendar.HOUR_OF_DAY);
String djhour = "";
if (hour < 10) {
djhour = "0" + Integer.toString(hour);
}
else {
djhour = Integer.toString(hour);
}
return djhour;
}
/*
#本函数作用是返回当前分钟
*/
public static String getMin() {
Calendar time = Calendar.getInstance();
int min = time.get(Calendar.MINUTE);
String djmin = "";
if (min < 10) {
djmin = "0" + Integer.toString(min);
}
else {
djmin = Integer.toString(min);
}
return djmin;
}
/*
#本函数的主要功能是格式化时间,以便于页面显示
#time 时间 可为6位、8位、12位、15位
#return 返回格式化后的时间
#6位 YY年MM月DD日
#8位 YYYY年MM月DD日
#12位 YY年MM月DD日 HH:II:SS
#15位 YY年MM月DD日 HH:II:SS:CCC
*/
public static String formattime(String time) {
int length = 0;
length = time.length();
String renstr = "";
switch (length) {
case 6:
renstr = time.substring(0, 2) + "年" + time.substring(2, 4) +
"月" + time.substring(4) + "日";
break;
case 8:
renstr = time.substring(0, 4) + "年" + time.substring(4, 6) +
"月" + time.substring(6, 8) + "日";
break;
case 12:
renstr = time.substring(0, 2) + "年" + time.substring(2, 4) +
"月" + time.substring(4, 6) + "日 " + time.substring(6, 8) +
"时" + time.substring(8, 10) + "分" +
time.substring(10, 12) + "秒";
break;
case 14:
renstr = time.substring(0, 4) + "-" + time.substring(4, 6) +
"-" + time.substring(6, 8) + " " + time.substring(8, 10) +
":" + time.substring(10, 12) + ":" +
time.substring(12, 14) + "";
break;
case 15:
renstr = time.substring(0, 2) + "年" + time.substring(2, 4) +
"月" + time.substring(4, 6) + "日 " + time.substring(6, 8) +
":" + time.substring(8, 10) + ":" +
time.substring(10, 12) + ":" + time.substring(12);
break;
default:
renstr = time.substring(0, 2) + "年" + time.substring(2, 4) +
"月" + time.substring(4) + "日";
break;
}
return renstr;
}
}
posted on 2006-09-19 19:32
xzc 阅读(278)
评论(0) 编辑 收藏 所属分类:
Java