Java开发常用方法汇总
2013-01-17 22:11
337 查看
时间的处理:
public static String getDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(new Date());
}
public static long getDateTime(String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return getDate(pattern, sdf.format(System.currentTimeMillis())).getTime();
}
public static String getDate(String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(new Date());
}
public static String getDate(String pattern, long times) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = new Date();
date.setTime(times);
return sdf.format(date);
}
public static String getDate(String pattern, Date date) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(date);
}
public static Date getDate(String pattern, String source) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
return sdf.parse(source);
} catch (ParseException e) {
}
return new Date();
}
如果对时间格式解析不正确可能是时区的问题,可以设置时区来解决。
private Date getDate(String pattern, String source){
SimpleDateFormat sdf = new SimpleDateFormat(pattern,new Locale("China"));
try {
return sdf.parse(source);
} catch (ParseException e) {
}
return new Date();
}
更多信息可以查询java.text.SimpleDateFormat类中对时间格式的定义
字符串的处理:
public static float str2float(String s, int defaultValue) {
try {
return Float.parseFloat(s);
} catch (Exception e) {
}
return defaultValue;
}
public static double str2double(String s, double defaultValue) {
try {
return Double.parseDouble(s);
} catch (Exception e) {
}
return defaultValue;
}
public static String obj2String(Object s, String defaultValue) {
try {
if (s != null)
return ((String) s).trim();
} catch (Exception e) {
}
return defaultValue;
}
public static int str2int(String s, int defaultValue) {
try {
if (s != null)
return Integer.parseInt(s.trim());
else
return defaultValue;
} catch (Exception e) {
return defaultValue;
}
}
public static long str2long(String s, long defaultValue) {
try {
if (s != null)
return Long.parseLong(s.trim());
else
return defaultValue;
} catch (Exception e) {
return defaultValue;
}
}
public static String getDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(new Date());
}
public static long getDateTime(String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return getDate(pattern, sdf.format(System.currentTimeMillis())).getTime();
}
public static String getDate(String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(new Date());
}
public static String getDate(String pattern, long times) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = new Date();
date.setTime(times);
return sdf.format(date);
}
public static String getDate(String pattern, Date date) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(date);
}
public static Date getDate(String pattern, String source) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
return sdf.parse(source);
} catch (ParseException e) {
}
return new Date();
}
如果对时间格式解析不正确可能是时区的问题,可以设置时区来解决。
private Date getDate(String pattern, String source){
SimpleDateFormat sdf = new SimpleDateFormat(pattern,new Locale("China"));
try {
return sdf.parse(source);
} catch (ParseException e) {
}
return new Date();
}
更多信息可以查询java.text.SimpleDateFormat类中对时间格式的定义
字符串的处理:
public static float str2float(String s, int defaultValue) {
try {
return Float.parseFloat(s);
} catch (Exception e) {
}
return defaultValue;
}
public static double str2double(String s, double defaultValue) {
try {
return Double.parseDouble(s);
} catch (Exception e) {
}
return defaultValue;
}
public static String obj2String(Object s, String defaultValue) {
try {
if (s != null)
return ((String) s).trim();
} catch (Exception e) {
}
return defaultValue;
}
public static int str2int(String s, int defaultValue) {
try {
if (s != null)
return Integer.parseInt(s.trim());
else
return defaultValue;
} catch (Exception e) {
return defaultValue;
}
}
public static long str2long(String s, long defaultValue) {
try {
if (s != null)
return Long.parseLong(s.trim());
else
return defaultValue;
} catch (Exception e) {
return defaultValue;
}
}
相关文章推荐
- Android开发java开发之常用英文词汇汇总。程序员必备英语单词
- Java Web 回顾总结(五) - HttpServlet - Request - 开发常用方法总结
- Java基础学习总结(70)——开发Java项目常用的工具汇总
- Java中正则表达式常用方法汇总
- yii项目开发项目常用技巧和方法汇总
- java开发中linux常用方法
- Java基础学习总结(70)——开发Java项目常用的工具汇总
- 常用SQL汇总,Java开发工作高频数据库语句
- Java开发之request对象常用方法整理
- Java String类学习(二)--String类常用方法汇总
- yii项目开发项目常用技巧和方法汇总之二
- Java中Map常用方法总结以及遍历方式的汇总
- javaweb中静态文件的常用处理方法汇总
- Java中Map常用方法总结以及遍历方式的汇总
- Java中request对象常用方法汇总
- yii项目开发项目常用技巧和方法汇总
- java开发中常用到小方法
- java 开发中常用方法
- Java-WEB开发常用方法整理
- Java中Map常用方法总结以及遍历方式的汇总