- 浏览: 54715 次
- 性别:
- 来自: 厦门
最新评论
/**
* 将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,) + 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,) - 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()));
}
}
发表评论
-
servlet jsp 的生命周期
2012-02-09 15:20 1440servlet的生命周期分为以下几个步骤, 第一,加载并实例化 ... -
反射内部类
2011-11-25 18:30 916package test; public class ... -
Date和Calendar类
2010-06-23 08:58 8011、具体类(和抽象类相对)java.util.Date 2、 ... -
Maven2和ivy比较
2010-06-22 13:14 1369Maven 2和Ivy常被放在一起对比,但实际上两者是不同 ... -
JAVA连接DB2注意事项
2010-05-15 23:21 1173使用Class.forName("COM.ibm. ... -
JAVA的MD5加密
2010-01-19 20:44 936MD5的全称是Message-Digest ... -
Web.xml的解析
2010-01-18 13:32 1024web.xml元素介绍 web.xml首先是肯定要包含它的 ... -
JAVA多线程
2010-01-18 13:23 689java.lang.Thread 和 java.lang.R ... -
JDBC调用存储过程
2010-01-09 16:17 900call 语法格式{call 存储过程名(参数列表)} ... -
java读取配置文件
2010-01-09 16:11 8321、随机id的产生如何实现 引入如下的包: impo ... -
UNICODE与汉字编码互转
2010-01-09 16:08 1597为了避免在浏览器中传输数据的时候出现中文乱码,我们可以将内 ... -
Eclipse中JVM内存设置
2010-01-09 16:06 629eclipse.ini内存设置 -vmarg ... -
Hibernate 中 执行 原生 SQL 语句
2010-01-09 16:04 3161用hibernate的executeQuery来执行SQL ... -
hibernate完成oracle存储过程(stored procedures)
2010-01-09 15:59 1137import java.sql.CallableState ... -
oracle 下hibernate3.2 sequence generator 生成id的问题
2010-01-09 15:57 1270@SequenceGenerator(name=&q ... -
hibernate三重关联
2009-12-25 12:15 1071------------------------------- ... -
iframe跨域访问cookie、Sessio
2009-12-23 22:20 19381、IE浏览器iframe跨域丢失Session问题 在开发 ... -
拦截系统关闭信号
2009-12-19 21:31 1042在Java窗口程序中按ctrl+c会强行中止Java程序。点击 ...
相关推荐
提供了很丰富的java工具类,包括字符串、数字、日期、文件、图像、编码解码、校验工具、文档操作等。 主要分为以下几种: - 1.通用操作类,例如String、数字、日期、各种校验等 - 2.文档操作,excel、pdf等 - 3.加密...
Java自动识别文件字符编码工具类 参考博客 https://blog.csdn.net/superbeyone/article/details/103036914 使用方式: String encode = EncodingDetect.getFileEncode(geoJsonFile); log.info("系统检测到文件[ {}...
提供了很丰富的java工具类,包括字符串、数字、日期、文件、图像、编码解码、校验工具、文档操作等。 主要分为以下几种: - 1.通用操作类,例如String、数字、日期、各种校验等 - 2.文档操作,excel、pdf等 - 3.加密...
文件工具类,Http请求工具类,图片处理工具类。...mail工具类,Map工具类,MD5编码工具类,数字工具类,随机数工具类,反射工具类,字符串处理工具类,URL工具类,XML工具类,常用的数据验证工具类
提供了很丰富的java工具类,包括字符串、数字、日期、文件、图像、编码解码、校验工具、文档操作等。 主要分为以下几种: - 1.通用操作类,例如String、数字、日期、各种校验等 - 2.文档操作,excel、pdf等 - 3.加密...
不需要关心接受的字符串编码是UTF_8还是GBK,还是ios-8859-1,自动转换为utf-8编码格式,无需判断字符串原有编码,用法://处理编码String newStr = GetEncode.transcode(oldStr);
字符编码工具类CharacterEncodingFilter.java 字符编码工具类CharacterEncodingFilter.java 字符编码工具类CharacterEncodingFilter.java
字符串工具类/数据类型转换类/集合工具类/数组工具类/Properties文件操作类/常用流操作工具类/编码工具类/Json工具类/日期工具类/下载文件工具类/解压ZIP工具类/文件编码转码
提供了很丰富的java工具类,包括字符串、数字、日期、文件、图像、编码解码、校验工具、文档操作等。 主要分为以下几种: - 1.通用操作类,例如String、数字、日期、各种校验等 - 2.文档操作,excel、pdf等 - 3.加密...
提供了很丰富的java工具类,包括字符串、数字、日期、文件、图像、编码解码、校验工具、文档操作等。 主要分为以下几种: - 1.通用操作类,例如String、数字、日期、各种校验等 - 2.文档操作,excel、pdf等 - 3.加密...
一些java常用的工具整合,包括 字符编码 ,获取服务器路径 ,获取类路径,数据库读取,支持自定义datatable类的数据库读取等类。
java基础工具类iceroot iceroot是一个java基础工具类.封装了很多有用的方法. 该类库无任何第三方依赖. 涵盖了 字符串操作 时间转化 读取配置文件 等方面. 基础工具类对于java代码的编写是非常必要的,然 而很多...
Base64工具类-字符编码工具类-数据类型转换-日期工具类-Escape中文转码工具类-fastjson工具类-文件工具类-Http工具类-http请求工具类-用于模拟HTTP请求中GET/POST方式 -图片处理工具类-Ip工具类-mail工具类-Map工具...
NULL 博文链接:https://sammyfun.iteye.com/blog/1662240
java常用的工具类收集;包括字符串、数字、日期、文件、图像、编码解码、校验工具、文档操作等。
提供了很丰富的java工具类,包括字符串、数字、日期、文件、图像、编码解码、校验工具、文档操作等。 主要分为以下几种: - 1.通用操作类,例如String、数字、日期、各种校验等 - 2.文档操作,excel、pdf等 - 3.加密...
JavaUtil类中其中包含的方法有: ...5.将文件转换为指定字符编码集的字符串 6.获取指定类的随机实例(传入Class模板) 7.根据指定包名搜索文件 如需使用如上方法,需要将JavaUtil复制到项目中任意位置即可
java 工具类,包含上传下载工具类,数据库工具类,验证码工具类,分页工具类,字符编码过滤工具类,字符串工具类 提醒:我已打包,在/WEB-INF/lib/itbofeng-utils.jar中,可以看到源码。 测试下载文件时修改servlet...
JAVA字符串操作类CTool.java字符转换类,此类中收集Java编程中WEB开发常用到的一些工具。为避免生成此类的实例,构造方法被申明为private类型的。封装的功能:字符串从GBK编码转换为Unicode编码、对字符串进行md5...
java过滤器实现统一字符编码 封装好的工具类,可以直接使用