Java常用工具类(字符编码,时间等)

/**
* 将UTF编码的字符串转化为GB2312编码的字符串,主要用来处理中文显示乱码的问题
*
* @param UTF
* 通过UTF编码的字符串
* @return 通过GB2312编码的字符串
*/
public static String GB2312FromUTF(String UTF) {
if (UTF == null) {
return "";
} else {
try {
return new String(UTF.getBytes("UTF-8"), "GB2312");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

/**
* 将GB2312编码的字符串转化为UTF-8编码的字符串,主要用来处理中文显示乱码的问题
*
* @param GB2312
* 通过GB2312编码的字符串
* @return 通过UTF-8编码的字符串
*/
public static String UTFFromGB2312(String GB2312) {
if (GB2312 == null) {
return "";
} else {
try {
return new String(GB2312.getBytes("GB2312"), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

public static String GBKFromISO8859_1(String ISO8859_1) {
if (ISO8859_1 == null) {
return "";
} else {
try {
return new String(ISO8859_1.getBytes("ISO8859_1"), "GBK");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

public static String GBKFromUTF(String UTF) {
if (UTF == null) {
return "";
} else {
try {
return new String(UTF.getBytes("UTF-8"), "GBK");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

/**
* 将ISO8859_1编码的字符串转化为UTF-8编码的字符串,主要用来处理中文显示乱码的问题
*
* @param ISO8859_1str
* 通过ISO8859_1编码的字符串
* @return 通过UTF-8编码的字符串
*/
public static String UTFFromISO8859_1(String ISO8859_1str) {
return ISO8859_1str;
}

public static String UTFFromGBK(String GBK) {
if (GBK == null) {
return "";
} else {
try {
return new String(GBK.getBytes("GBK"), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

/**
* 将UTF-8编码的字符串转化为ISO8859_1编码的字符串,主要用来处理中文显示乱码的问题
*
* @param UTF
* 通过UTF编码的字符串
* @return 通过ISO8859_1编码的字符串
*/
public static String ISO8859_1FromUTF(String UTFstr) {
if (UTFstr == null) {
return "";
} else {
try {
return new String(UTFstr.getBytes("UTF-8"), "ISO8859_1");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

/**
* 将GB2312编码的字符串转化为ISO8859_1编码的字符串
*
* @param GBstr
* GB2312编码的字符串
* @return ISO8859_1编码的字符串
*/
public static String ISO8859_1String(String GBstr) {
if (GBstr == null) {
return "";
} else {
try {
return new String(GBstr.getBytes("GB2312"), "ISO8859_1");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

/**
* 将GB2312编码的字符串转化为ISO8859_1编码的字符串
*
* @param GBstr
* GB2312编码的字符串
* @return ISO8859_1编码的字符串
*/
public String ISO8859_1FromGB2312(String GBstr) {
if (GBstr == null) {
return "";
} else {
try {
return new String(GBstr.getBytes("GB2312"), "ISO8859_1");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

public static String ISO8859_1FromGBK(String GBK) {
if (GBK == null) {
return "";
} else {
try {
return new String(GBK.getBytes("GBK"), "ISO8859_1");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

/**
* 去除字符串两端空格。
*
* @param str
* 需要处理的字符串
* @return 去掉了两端空格的字符串,如果str 为 null 则返回 ""
*/
public static String trim(String str) {
if (str != null) {
return str.trim();
} else {
return "";
}
}

// static public String mm_dd_yyyy = "MM-dd-yyyy HH:mm:ss";
/**
* 获得当前年份
*
* @return 当前年份,格式如:2003
*/
public static int getCurrentYear() {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy");
return Integer.parseInt(sdf.format(new java.util.Date()));
}

/**
* 获得当前月份
*
* @return 当前月份
*/
public static int getCurrentMonth() {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("M");
return Integer.parseInt(sdf.format(new java.util.Date()));
}

/**
* 获得当前天
*
* @return 当前天
*/
public static int getCurrentDay() {
Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.DATE);
}

public static String getCurrentDateTime() {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
"yyyy-MM-dd H:mm");
return sdf.format(new Date());
}

/**
* 获得形如 19770608 格式的当前年月日
*
* @return 当前年月日
*/
public static String getSimpleCurrentDate() {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
"yyyyMMdd HH:mm:ss");
return sdf.format(new java.util.Date());
}

/**
* 返回两个日期相差天数
*
* @param d1
* 日期
* @param d2
* 日期
* @return 天数
*/
public int diffDate(Date d1, Date d2) {
if ((d1 == null) || (d2 == null))
return 0;

Calendar cal = Calendar.getInstance();

// from Locale, has nothing to do with your input date format
int zoneoffset = cal.get(Calendar.ZONE_OFFSET);
int dstoffset = cal.get(Calendar.DST_OFFSET);

// getTime() return absolute GMT time
// compensate with the offsets
long dl1 = d1.getTime() + zoneoffset + dstoffset;
long dl2 = d2.getTime() + zoneoffset + dstoffset;

int intDaysFirst = (int) (dl1 / (60 * 60 * 1000 * 24)); //60*60*1000
int intDaysSecond = (int) (dl2 / (60 * 60 * 1000 * 24));

return intDaysFirst > intDaysSecond ? intDaysFirst - intDaysSecond
: intDaysSecond - intDaysFirst;
}

/**
* 将给定的时间转换为格式是8位的字符串
*
* @param date
* 给定的时间
* @return 格式化后的字符串形式的时间
*/
public String get8BitDate(java.util.Date date) {
if (date == null) {
return "";
}
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
"yyyyMMdd");
return sdf.format(date);
}

public String to_date(String strdate, String df) {
if (strdate == null) {
return "";
}
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
java.text.SimpleDateFormat sdf1 = new java.text.SimpleDateFormat(
"M/d/yyyy H:m:s");
Date d = null;
try {
d = sdf1.parse(strdate);
} catch (ParseException ex) {
ex.printStackTrace();
}
return sdf.format(d);
}

public static String get8BitString(String strDate) {
if (strDate == null) {
return "";
}
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
java.text.SimpleDateFormat sdf2 = new java.text.SimpleDateFormat(
"yyyyMMdd");
Date d = null;
try {
d = sdf.parse(strDate);
} catch (ParseException ex) {
ex.printStackTrace();
}
return sdf2.format(d);
}

public static String get8ByteTo10Byte(String strDate) {
if (strDate == null) {
return "";
}
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
"yyyyMMdd");
java.text.SimpleDateFormat sdf2 = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
Date d = null;
try {
d = sdf.parse(strDate);
} catch (ParseException ex) {
ex.printStackTrace();
}
return sdf2.format(d);
}

public static String getStandedDateTime(String strDate) {
if (strDate == null) {
return "";
}
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
java.text.SimpleDateFormat sdf2 = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
Date d = null;
try {
d = sdf.parse(strDate);
} catch (ParseException ex) {
ex.printStackTrace();
}
return sdf2.format(d);
}

public static String getMonthDay(java.util.Date date) {
if (date == null) {
return "";
}
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("M月d日");
return sdf.format(date);
}

public static String getHourMinute(java.util.Date date) {
if (date == null) {
return "";
}
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("H:mm");
return sdf.format(date);
}

/**
* 判断字符串是否符合日期格式
*
* @param str
* 字符串时间
* @return
*/
public static boolean isDate(String strDate) {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
sdf.setLenient(false);
try {
sdf.parse(strDate);
return true;
} catch (ParseException ex) {
return false;
}
}

/**
* 判断是否是数字
*
* @param str
* @return
*/
public static boolean isNumber(String strNumber) {
boolean bolResult = false;
try {
Double.parseDouble(strNumber);
bolResult = true;
} catch (NumberFormatException ex) {
bolResult = false;
}
return bolResult;
}

public String dateadd(Date strDate, int a) {
String str = "";

java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
String strDate1 = sdf.format(strDate);

int year = Integer.parseInt(strDate1.substring(0, 4));
int month = Integer.parseInt(strDate1.substring(5, 7));
int day = Integer.parseInt(strDate1.substring(8, 10));
int md = getdayformonth(month);
int i = (day + a) / md;
int j = (day + a) % md;
if (j == 0) {
i = i - 1;
j = md;
}
String strmon = "";
String strday = "";
String mondiff = "";
if (i < 2) {
if (Integer.toString(j).length() == 1) {
strday = "0" + Integer.toString(j);
} else {
strday = Integer.toString(j);
}
if ((month + i) > 12) {
int yeardiff = (month + i) / 12;
int monthmod = (month + i) % 12;
mondiff = Integer.toString(monthmod);
if (Integer.toString(monthmod).length() == 1) {
mondiff = "0" + Integer.toString(monthmod);
}
str = Integer.toString(year + yeardiff) + "-" + mondiff + "-"
+ strday;
} else {
strmon = Integer.toString(month + i);
if (Integer.toString(month + i).length() == 1) {
strmon = "0" + Integer.toString(month + i);
}

str = Integer.toString(year) + "-" + strmon + "-" + strday;

}
} else {
//主要判断假如天数,月份溢出的处理,
}
return str;
}

public int getdayformonth(int month) {
int a = 0;
switch (month) {
case 1:
a = 31;
break;
case 2:
a = 28;
break;
case 3:
a = 31;
break;
case 4:
a = 30;
break;
case 5:
a = 31;
break;
case 6:
a = 30;
break;
case 7:
a = 31;
break;
case 8:
a = 31;
break;
case 9:
a = 30;
break;
case 10:
a = 31;
break;
case 11:
a = 30;
break;
case 12:
a = 31;
break;
default:

}
return a;
}

public String addOneDay(String strDate) //YYYY-MM-DD
{
int[] standardDays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int[] leapyearDays = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int y = Integer.parseInt(strDate.substring(0, 4));
int m = Integer.parseInt(strDate.substring(4, 6));
int d = Integer.parseInt(strDate.substring(6, 8)) + 1;
int maxDateCount = 0;

System.out.println(y);
System.out.println(m);
System.out.println(d);

if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
maxDateCount = leapyearDays[m - 1];
} else {
maxDateCount = standardDays[m - 1];
}

if (d > maxDateCount) {
d = 1;
m++;
}

if (m > 12) {
m = 1;
y++;
}
java.text.DecimalFormat yf = new java.text.DecimalFormat("0000");
java.text.DecimalFormat mdf = new java.text.DecimalFormat("00");
return yf.format(y) + mdf.format(m) + mdf.format(d);
}

public static String subOneDay(String strDate) {
//YYYY-MM-DD
int[] standardDays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int[] leapyearDays = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int y = Integer.parseInt(strDate.substring(0, 4));
int m = Integer.parseInt(strDate.substring(4, 6));
int d = Integer.parseInt(strDate.substring(6, 8)) - 1;
int maxDateCount = 0;

System.out.println(y);
System.out.println(m);
System.out.println(d);

if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
maxDateCount = leapyearDays[m - 1];
} else {
maxDateCount = standardDays[m - 1];
}

if (d > maxDateCount) {
d = 1;
m++;
}

if (m > 12) {
m = 1;
y++;
}
java.text.DecimalFormat yf = new java.text.DecimalFormat("0000");
java.text.DecimalFormat mdf = new java.text.DecimalFormat("00");
return yf.format(y) + mdf.format(m) + mdf.format(d);
}

public static void main(String[] argv) {
System.out.println(Tools.getMonthDay(new java.util.Date()));
System.out.println(Tools.getHourMinute(new java.util.Date()));

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值