您的位置:首页 > 编程语言 > Java开发

数据类型的转化和判断方法

2015-04-21 09:14 211 查看
1.判断字符串是否为可用的数字(如在关于钱的数字上)

public static String getValidNum(String str) {
try {
return new BigDecimal(str.trim()).toString();
} catch (Exception e) {
return -1+"";
}
}

把字符串转化为数字(精度要求较高),不能转化的默认为-1。

下面一个是关于 时间的转化判断,具体的方法说明就不多说了

import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
@SuppressWarnings({ "unchecked", "rawtypes" })
public class DateUtil {
public static String DATE_FORMAT = "yyyy-MM-dd";
public static String DATE_FORMAT_ENUS = "MM/dd/yyyy";
public static String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static String DATETIME_FORMAT_NO_Second = "yyyy-MM-dd HH:mm";
public static String TIME_FORMAT = "HH:mm";
//中文冒号
public static String TIME_FORMAT_CN = "HH:mm";
/**
* 用指定格式解析指定字串为Date类型
* @param str
* @param format
* @return
*/
public static Date getDateFromString(String str, String format) {
try {
return new SimpleDateFormat(format).parse(str);
} catch (ParseException e) {
;
}

return null;
}

/**
* 判断字符串是否为时间
* @param str
* @return 时间字符串(yyyy-MM-dd HH:mm:ss) 不是时间字符串时返回 null
*/
public static String getValidDate(String str) {
SimpleDateFormat format=new SimpleDateFormat(DATETIME_FORMAT);
Date date=null;
try{
format.setLenient(false);
date = format.parse(str);
}catch(Exception e){
format=new java.text.SimpleDateFormat(DATETIME_FORMAT_NO_Second);
try {
format.setLenient(false);
date = format.parse(str);
} catch (ParseException e1) {
format=new java.text.SimpleDateFormat(DATE_FORMAT);
try {
format.setLenient(false);
date=format.parse(str);
} catch (Exception e2) {
return null;
}
}
}
SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_FORMAT);
return sdf.format(date);
}

//----------------------------------获取特定日期
/**
* <p>
* Description:字串6位,前4代表年,后2代表月,<br>
* 返回给定日期中的月份中的最后一天
* </p>
* param term(YYYYMMDD)
*
* @return String
*/
public static String getLastDay(String term) {
int getYear = Integer.parseInt(term.substring(0, 4));
int getMonth = Integer.parseInt(term.substring(4, 6));

String getLastDay = "";

if (getMonth == 2) {
if (getYear % 4 == 0 && getYear % 100 != 0 || getYear % 400 == 0) {
getLastDay = "29";
} else {
getLastDay = "28";
}
} else if (getMonth == 4 || getMonth == 6 || getMonth == 9
|| getMonth == 11) {
getLastDay = "30";
} else {
getLastDay = "31";
}
return String.valueOf(getYear) + "年" + term.substring(4, 6) + "月"
+ getLastDay + "日";
}

/**
* <p>
* 获得本周周一的日期
* </p>
*
* @param date
* @return yyyy-MM-dd格式的日期字符串
* @throws ParseException
*/
public static String getMondayOfThisWeek(String date) throws ParseException {
return getDayOfTheWeek(reviseSundayBug(date), "yyyy-MM-dd",
Calendar.MONDAY);
}

/**
* <p>
* 获得本周周日的日期
* </p>
*
* @param date
* @return yyyy-MM-dd格式的日期字符串
* @throws ParseException
*/
public static String getSundayOfThisWeek(String date) throws ParseException {
if (getDayOfTheWeek(date) == Calendar.SUNDAY) {
Calendar c = Calendar.getInstance();
c.setTime(parseDateStr(date, "yyyy-MM-dd"));
c.add(Calendar.DATE, -7);
date = format(c.getTime(), "yyyy-MM-dd");
}
return getDayOfTheWeek(reviseSundayBug(date), "yyyy-MM-dd",
Calendar.SUNDAY);
}

/**
* <p>
* 修正当前日期是周日时计算本周日期的bug
* </p>
*
* @param date
* @return
* @throws ParseException
*/
private static String reviseSundayBug(String date) throws ParseException {
if (getDayOfTheWeek(date) == Calendar.SUNDAY) {
Calendar c = Calendar.getInstance();
c.setTime(parseDateStr(date, "yyyy-MM-dd"));
c.add(Calendar.DATE, -7);
date = format(c.getTime(), "yyyy-MM-dd");
}
return date;
}

/**
* <p>
* 解析时间日期字符串
* </p>
*
* @param date
* @param format
* @return
* @throws ParseException
*/
public static Date parseDateStr(String date, String format)
throws ParseException {
DateFormat df = new SimpleDateFormat(format);
try {
return df.parse(date);
} catch (ParseException e) {
e.printStackTrace();
throw e;
}
}

/**
* <p>
* 获得一周七天的日期
* </p>
*
* @param date
* @return
* @throws ParseException
*/
public static String[] getOneWeekDates(String date) throws ParseException {
return getOneWeekDates(reviseSundayBug(date), "yyyy-MM-dd");
}

/**
* <p>
* 按照指定格式日期获得一周七天的日期
* </p>
*
* @param date
* @param df
* @return
* @throws ParseException
*/
public static String[] getOneWeekDates(String date, String df)
throws ParseException {
String[] dates = new String[7];
dates[0] = getDayOfTheWeek(date, df, Calendar.MONDAY);
dates[1] = getDayOfTheWeek(date, df, Calendar.TUESDAY);
dates[2] = getDayOfTheWeek(date, df, Calendar.WEDNESDAY);
dates[3] = getDayOfTheWeek(date, df, Calendar.THURSDAY);
dates[4] = getDayOfTheWeek(date, df, Calendar.FRIDAY);
dates[5] = getDayOfTheWeek(date, df, Calendar.SATURDAY);
dates[6] = getDayOfTheWeek(date, df, Calendar.SUNDAY);
return dates;
}

/**
* <p>
* 获得上一周的日期
* </p>
*
* @param date
* 格式形如"YYYY-MM-DD"的字符串
* @return
*/
public static String getLastWeekDate(String date) {
Date d = stringToDate(date, "yyyy-mm-dd");
Calendar calo = Calendar.getInstance();
calo.setTime(d);
calo.add(Calendar.DATE, -7);

return dateToString(d, "yyyy-mm-dd");
}

/**
* <p>
* 获得下一周的日期
* </p>
*
* @param date
* 格式形如"YYYY-MM-DD"的字符串
* @return
*/
public static String getNextWeekDate(String date) {
Date d = stringToDate(date, "yyyy-mm-dd");
Calendar calo = Calendar.getInstance();
calo.setTime(d);
calo.add(Calendar.DATE, 7);

return dateToString(d, "yyyy-mm-dd");
}

/**
* <p>
* 获得指定日期所在周的周几的日期
* </p>
*
* @param date
* 字符串格式的日期
* @param df
* 日期的格式化字符串,如yyyy-MM-dd
* @param DAY_OF_WEEK
* 周几,如Calendar.MONDAY
* @return
* @throws ParseException
*/
public static String getDayOfTheWeek(String date, String df, int DAY_OF_WEEK)
throws ParseException {
Calendar c = Calendar.getInstance();
c.setTime(parseDateStr(date, df));
c.set(Calendar.DAY_OF_WEEK, DAY_OF_WEEK);
// 中国习惯是周日是一个星期的最后一天
if (DAY_OF_WEEK == Calendar.SUNDAY) {
c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
c.add(Calendar.DATE, 1);
}
return new SimpleDateFormat(df).format(c.getTime());
}

/**
* <p>
* 获得当前日期是周几
* </p>
*
* @param date
* @return 周几在Calendar类中对应的静态变量值
* @throws ParseException
*/
public static int getDayOfTheWeek(String date) throws ParseException {
Calendar c = Calendar.getInstance();
c.setTime(parseDateStr(date, "yyyy-MM-dd"));
return c.get(Calendar.DAY_OF_WEEK);
}

/**
* <p>
* 获得当前日期是周几
* </p>
*
* @param date
* @return
* @throws ParseException
*/
public static int getDayOfTheWeek(Date date) throws ParseException {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c.get(Calendar.DAY_OF_WEEK);
}

/**
* <p>
* 获得当前日期是周几
* </p>
*
* @param date
* @param DAY_OF_WEEK
* @return
* @throws ParseException
*/
public static Date getDayOfTheWeek(Date date, int DAY_OF_WEEK)
throws ParseException {
Calendar c = Calendar.getInstance();
c.setTime(date);

c.set(Calendar.DAY_OF_WEEK, DAY_OF_WEEK);
// 中国习惯是周日是一个星期的最后一天
if (DAY_OF_WEEK == Calendar.SUNDAY) {
c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
c.add(Calendar.DATE, 1);
}

return c.getTime();
}

/**
* <p>
* 得到当前日期指定周日期
* </p>
*
* @param date
* @param DAY_OF_WEEK
* @return
* @throws ParseException
*/
public static Date getDayFromDate(Date date, int DAY_OF_WEEK)
throws ParseException {
Calendar c = Calendar.getInstance();
date = getDayOfTheWeek(date, DAY_OF_WEEK);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, DAY_OF_WEEK);
return c.getTime();
}

/**
* <p>
* 得到当前日期那周周一日期
* </p>
*
* @param date
* @return
* @throws ParseException
*/
public static Date getMondayFromDate(Date date) throws ParseException {
return getDayFromDate(date, Calendar.MONDAY);
}

/**
* <p>
* 得到当前日期那周周一日期的字符串
* </p>
*
* @param date
* @return
* @throws ParseException
*/
public static String getMondayStrFromDate(Date date, String pattern)
throws ParseException {
return format(getMondayFromDate(date), pattern);
}

/**
* <p>
* 得到当前日期那周周日日期
* </p>
*
* @param date
* @return
* @throws ParseException
*/
public static Date getSundayFromDate(Date date) throws ParseException {
return getDayFromDate(date, Calendar.SUNDAY);
}

/**
* <p>
* 得到当前日期那周周日日期的字符串
* </p>
*
* @param date
* @return
* @throws ParseException
*/
public static String getSundayStrFromDate(Date date, String pattern)
throws ParseException {
return format(getSundayFromDate(date), pattern);
}

//-----------------------把date转成指定格式的String
/**
* format 格式化date
* @param date
* @param pattern
* 指定格式:"yyyy-MM-dd HH:mm:ss" "yyyy年MM月dd日kk时mm分" 的各种组合 Integer.parseInt
* @return
*/
public static String format(Date date, String pattern) {
if (date == null) {
return "N/A";
}
SimpleDateFormat format = new SimpleDateFormat(pattern);
return format.format(date);
}

/**
* format 将字符串转换为日期型
* @param strDate 日期字符串
* @param pattern 指定格式
* @return Date
* @throws Exception
*/
public static Date format(String strDate, String pattern) throws ParseException {

SimpleDateFormat sdf=new SimpleDateFormat();
sdf.applyPattern(pattern);
return sdf.parse(strDate);
}
//-----------------------获取当前日期
/**
* getCurrentDate 获取当前日期
* @return Date
* @throws Exception
*/
public static Date getCurrentDate(){
Calendar cal = Calendar.getInstance();
Date d = cal.getTime();
return d;
}

/**
* getCurrentDateStr 获取当前日期的字符串
* @param pattern 日期格式
* @return String
* @throws Exception
*/
public static String getCurrentDateStr(String pattern){
return format(getCurrentDate(),pattern);
}
//----------------------------------两个日期之间的计算
/**
* yearsBetweenDates 得到两个日期之间相差的年数
* @param newDate 新日期
* @param oldDate 旧日期
* @return int 新日期大,正数;否则负数
* @throws Exception
*/
public static int yearsBetweenDates(Date newDate, Date oldDate) {
Calendar start = Calendar.getInstance();
start.setTime(newDate);
Calendar end = Calendar.getInstance();
end.setTime(oldDate);
int y=end.get(Calendar.YEAR)-start.get(Calendar.YEAR);
return y;
}

/**
* monthsBetweenDates 得到两个日期之间相差的月数
* @param newDate 新日期
* @param oldDate 旧日期
* @return int 新日期大,正数;否则负数
* @throws Exception
*/
public static int monthsBetweenDates(Date newDate, Date oldDate) {
Calendar start = Calendar.getInstance();
start.setTime(newDate);
Calendar end = Calendar.getInstance();
end.setTime(oldDate);
int y=end.get(Calendar.YEAR)-start.get(Calendar.YEAR);
int m=y*12 + end.get(Calendar.MONTH)-start.get(Calendar.MONTH);
return m;
}

/**
* daysBetweenDates 得到两个日期之间相差的天数
* @param newDate 新日期
* @param oldDate 旧日期
* @return int 新日期大,正数;否则负数
* @throws Exception
*/
public static int daysBetweenDates(Date newDate, Date oldDate) {
long time = newDate.getTime() - oldDate.getTime();
int day = (int) (time / (24 * 3600 * 1000));
return day;
}
//----------------------------------日期增加年、月和日的计算
/**
* getDateBetween 取得与原日期相差一定天数的日期,返回Date型日期
* @param date
* @param intBetween
* @return Date
* @throws Exception
*/
public static Date getDateIncreaseDays(Date date, int intBetween) {
Calendar calo = Calendar.getInstance();
calo.setTime(date);
calo.add(Calendar.DATE, intBetween);
return calo.getTime();
}

/**
* getDateIncreaseMonths 取得与原日期相差一定月数的日期,返回Date型日期
* @param date
* @param addMonth
* @return Date
* @throws Exception
*/
public static Date getDateIncreaseMonths(Date date, int addMonth) {
Calendar calo = Calendar.getInstance();
calo.setTime(date);
calo.add(Calendar.MONTH, addMonth);
return calo.getTime();
}

/**
* getDateIncreaseYears 取得与原日期相差一定年数的日期,返回Date型日期
* @param date
* @param addMonth
* @return Date
* @throws Exception
*/
public static Date getDateIncreaseYears(Date date, int addMonth) {
Calendar calo = Calendar.getInstance();
calo.setTime(date);
calo.add(Calendar.YEAR, addMonth);
return calo.getTime();
}

/**
* <p>
* 返回java.sql.Date类型的日期
* </p>
*
* @param d
* @return
*/
public static java.sql.Date getSqlDate(Date d) {
if (d != null) {
java.sql.Date a = new java.sql.Date(d.getTime());
return a;
} else {
return null;
}
}

/**
* <p>
* 把数据库中的日期(2005-03-29 00:00:00.0)转化成java.util.Date型
* </p>
*
* @param sqlstr
* @return
* @throws Exception
*/
public static java.util.Date getStrUtilDate(String sqlstr) throws Exception {
java.util.Date d = null;
if (sqlstr == null || sqlstr.equals("") || sqlstr.length() <= 0)
return d;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

try {
if (sqlstr.length() < 18) {
sqlstr = sqlstr + " 00:00:00";
}
d = formatter.parse(sqlstr.substring(0, 18));
} catch (ParseException e) {
throw new Exception("在转换" + sqlstr + "为java.util.Date型出错"
+ e.getMessage());
}
return d;
}

/**
* <p>
* 把日期(2005-03-29 00:00:00.0)转化成java.util.Date(2005-03-29 00:00:00.0)型
* </p>
*
* @param sqlstr
* @return
* @throws Exception
*/
public static java.util.Date convertStrToDate(String sqlstr)
throws Exception {
java.util.Date d = null;
if (sqlstr == null || sqlstr.equals("") || sqlstr.length() <= 0)
return d;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
d = formatter.parse(sqlstr);
} catch (ParseException e) {
throw new Exception("在转换" + sqlstr + "为java.util.Date型出错"
+ e.getMessage());
}
return d;
}

/**
* <p>
* 判断日期date是否不早于当前日期
* </p>
*
* @param date
* @return 只要date不早于当前日期就返回true,否则返回false
* @throws ParseException
*/
public static boolean isNotBeforeToday(String date) throws ParseException {
Date compareDate = parseDateStr(date, "yyyy-MM-dd");
return isToday(date) ? true : compareDate.after(new Date());
}

/**
* <p>
* 判断字符串是否为当天日期
* </p>
*
* @param date
* @return
*/
public static boolean isToday(String date) {
return date.equals(format(new Date(), "yyyy-MM-dd"));
}

/**
* <p>
* Description:将字符串转换为日期型
* </p>
*
* @param strDate
* @param format
* --oracle型日期格式
* @return
*/
public static Date stringToDate(String strDate, String oracleFormat) {

if (strDate == null)
return null;
Hashtable h = new Hashtable();
String javaFormat = new String();
String s = oracleFormat.toLowerCase();
if (s.indexOf("yyyy") != -1)
h.put(new Integer(s.indexOf("yyyy")), "yyyy");
else if (s.indexOf("yy") != -1)
h.put(new Integer(s.indexOf("yy")), "yy");
if (s.indexOf("mm") != -1)
h.put(new Integer(s.indexOf("mm")), "MM");

if (s.indexOf("dd") != -1)
h.put(new Integer(s.indexOf("dd")), "dd");
if (s.indexOf("hh24") != -1)
h.put(new Integer(s.indexOf("hh24")), "HH");
if (s.indexOf("mi") != -1)
h.put(new Integer(s.indexOf("mi")), "mm");
if (s.indexOf("ss") != -1)
h.put(new Integer(s.indexOf("ss")), "ss");

int intStart = 0;
while (s.indexOf("-", intStart) != -1) {
intStart = s.indexOf("-", intStart);
h.put(new Integer(intStart), "-");
intStart++;
}

intStart = 0;
while (s.indexOf("/", intStart) != -1) {
intStart = s.indexOf("/", intStart);
h.put(new Integer(intStart), "/");
intStart++;
}

intStart = 0;
while (s.indexOf(" ", intStart) != -1) {
intStart = s.indexOf(" ", intStart);
h.put(new Integer(intStart), " ");
intStart++;
}

intStart = 0;
while (s.indexOf(":", intStart) != -1) {
intStart = s.indexOf(":", intStart);
h.put(new Integer(intStart), ":");
intStart++;
}

if (s.indexOf("年") != -1)
h.put(new Integer(s.indexOf("年")), "年");
if (s.indexOf("月") != -1)
h.put(new Integer(s.indexOf("月")), "月");
if (s.indexOf("日") != -1)
h.put(new Integer(s.indexOf("日")), "日");
if (s.indexOf("时") != -1)
h.put(new Integer(s.indexOf("时")), "时");
if (s.indexOf("分") != -1)
h.put(new Integer(s.indexOf("分")), "分");
if (s.indexOf("秒") != -1)
h.put(new Integer(s.indexOf("秒")), "秒");

int i = 0;
while (h.size() != 0) {
Enumeration e = h.keys();
int n = 0;
while (e.hasMoreElements()) {
i = ((Integer) e.nextElement()).intValue();
if (i >= n)
n = i;
}
String temp = (String) h.get(new Integer(n));
h.remove(new Integer(n));

javaFormat = temp + javaFormat;
}
// System.out.println(javaFormat);
SimpleDateFormat df = new SimpleDateFormat(javaFormat);

Date myDate = new Date();
try {
myDate = df.parse(strDate);
} catch (Exception e) {
return null;
}

return myDate;
}

/**
* <p>
* Description:获取输入格式的日期字符串,字符串遵循Oracle格式
* </p>
*
* @param d
* @param format
* @return
*/
public static String dateToString(Date d, String format) {
if (d == null)
return "";

Hashtable h = new Hashtable();
String javaFormat = new String();
String s = format.toLowerCase();
if (s.indexOf("yyyy") != -1)
h.put(new Integer(s.indexOf("yyyy")), "yyyy");
else if (s.indexOf("yy") != -1)
h.put(new Integer(s.indexOf("yy")), "yy");
if (s.indexOf("mm") != -1)
h.put(new Integer(s.indexOf("mm")), "MM");

if (s.indexOf("dd") != -1)
h.put(new Integer(s.indexOf("dd")), "dd");
if (s.indexOf("hh24") != -1)
h.put(new Integer(s.indexOf("hh24")), "HH");
if (s.indexOf("mi") != -1)
h.put(new Integer(s.indexOf("mi")), "mm");
if (s.indexOf("ss") != -1)
h.put(new Integer(s.indexOf("ss")), "ss");

int intStart = 0;
while (s.indexOf("-", intStart) != -1) {
intStart = s.indexOf("-", intStart);
h.put(new Integer(intStart), "-");
intStart++;
}

intStart = 0;
while (s.indexOf("/", intStart) != -1) {
intStart = s.indexOf("/", intStart);
h.put(new Integer(intStart), "/");
intStart++;
}

intStart = 0;
while (s.indexOf(" ", intStart) != -1) {
intStart = s.indexOf(" ", intStart);
h.put(new Integer(intStart), " ");
intStart++;
}

intStart = 0;
while (s.indexOf(":", intStart) != -1) {
intStart = s.indexOf(":", intStart);
h.put(new Integer(intStart), ":");
intStart++;
}

if (s.indexOf("年") != -1)
h.put(new Integer(s.indexOf("年")), "年");
if (s.indexOf("月") != -1)
h.put(new Integer(s.indexOf("月")), "月");
if (s.indexOf("日") != -1)
h.put(new Integer(s.indexOf("日")), "日");
if (s.indexOf("时") != -1)
h.put(new Integer(s.indexOf("时")), "时");
if (s.indexOf("分") != -1)
h.put(new Integer(s.indexOf("分")), "分");
if (s.indexOf("秒") != -1)
h.put(new Integer(s.indexOf("秒")), "秒");

int i = 0;
while (h.size() != 0) {
Enumeration e = h.keys();
int n = 0;
while (e.hasMoreElements()) {
i = ((Integer) e.nextElement()).intValue();
if (i >= n)
n = i;
}
String temp = (String) h.get(new Integer(n));
h.remove(new Integer(n));

javaFormat = temp + javaFormat;
}
SimpleDateFormat df = new SimpleDateFormat(javaFormat,
new DateFormatSymbols());

return df.format(d);
}

/**
* <p>
* 取得与原日期相差一定天数的日期,返回Date型日期
* </p>
*
* @param date
* @param intBetween
* @return
*/
public static Date getDateBetween(Date date, int intBetween) {
Calendar calo = Calendar.getInstance();
calo.setTime(date);
calo.add(Calendar.DATE, intBetween);
return calo.getTime();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java tool