1. 问题:
String dateStr = “2005-11-07 11:22:55”;
如何将字符串转为Date型?
解决办法:
DateFormat f = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");
Datea d = f.parse("2005-11-07 11:22:55");
2. 如何将日期转为字符串?
先用parse转成date型,再用format转成string。
public static void main(String[] args) throws Exception{
Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2005-11-07");
String now = new SimpleDateFormat("yyyy年MM月dd日").format(date);
System.out.println(now);
}
3. 如何限制时间格式输出?
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd-HH.mm.ss");
yyyy.MM.dd-HH.mm.ss 是 要输出的格式
String d = sdf.parse(new Date());
4. 如何生成随机日期?(这个是转载的:http://blog.csdn.net/opnmzxcvb/article/details/3970683)
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateRandomTest {
// 返回2007-01-01到2007-03-01的一个随机日期
public static void main(String[] args) {
Date randomDate = randomDate("2007-01-01", "2007-03-01");
System.out.println(randomDate.toString());
}
/**
* 获取随机日期
*
* @param beginDate
* 起始日期,格式为:yyyy-MM-dd
* @param endDate
* 结束日期,格式为:yyyy-MM-dd
* @return
*/
private static Date randomDate(String beginDate, String endDate) {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date start = format.parse(beginDate);// 构造开始日期
Date end = format.parse(endDate);// 构造结束日期
// getTime()表示返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
if (start.getTime() >= end.getTime()) {
return null;
}
long date = random(start.getTime(), end.getTime());
return new Date(date);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static long random(long begin, long end) {
long rtn = begin + (long) (Math.random() * (end - begin));
// 如果返回的是开始时间和结束时间,则递归调用本函数查找随机值
if (rtn == begin || rtn == end) {
return random(begin, end);
}
return rtn;
}
}
5. 一个日期的工具类(转载于:http://bbs.csdn.net/topics/80160200)
package com.oursoft.callcenter.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 日期互转
* @author dhm
*
*/
public class DateFormat {
public static final String FORMAT_STRING="yyyy-MM-dd HH:mm:ss";
private static SimpleDateFormat format=new SimpleDateFormat(FORMAT_STRING);
/**
* 字符转日期
* @param date
* @return
*/
public static Date stringToDate(String date){
try {
return format.parse(date);
} catch (ParseException e) {
return null;
}
}
/**
* 日期转字符
* @param date
* @return
*/
public static String dateToString(Date date){
SimpleDateFormat format=new SimpleDateFormat(FORMAT_STRING);
return format.format(date);
}
}
6. Java获取系统当前时间的方法并指定格式,转载于:http://outofmemory.cn/code-snippet/15748/Java-get-system-time-method-specify-format
/**
* 返回当前日期时间字符串<br>
* 默认格式:yyyy-mm-dd hh:mm:ss
*
* @return String 返回当前字符串型日期时间
*/
public static String getCurrentTime() {
String returnStr = null;
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
returnStr = f.format(date);
return returnStr;
}
/**
* 返回当前日期时间字符串<br>
* 默认格式:yyyymmddhhmmss
*
* @return String 返回当前字符串型日期时间
*/
public static BigDecimal getCurrentTimeAsNumber() {
String returnStr = null;
SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
returnStr = f.format(date);
return new BigDecimal(returnStr);
}
/**
* 返回自定义格式的当前日期时间字符串
*
* @param format
* 格式规则
* @return String 返回当前字符串型日期时间
*/
public static String getCurrentTime(String format) {
String returnStr = null;
SimpleDateFormat f = new SimpleDateFormat(format);
Date date = new Date();
returnStr = f.format(date);
return returnStr;
}
/**
* 返回当前字符串型日期
*
* @return String 返回的字符串型日期
*/
public static String getCurDate() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd");
String strDate = simpledateformat.format(calendar.getTime());
return strDate;
}
/**
* 返回指定格式的字符型日期
* @param date
* @param formatString
* @return
*/
public static String Date2String(Date date, String formatString) {
if (G4Utils.isEmpty(date)) {
return null;
}
SimpleDateFormat simpledateformat = new SimpleDateFormat(formatString);
String strDate = simpledateformat.format(date);
return strDate;
}
/**
* 返回当前字符串型日期
*
* @param format
* 格式规则
*
* @return String 返回的字符串型日期
*/
public static String getCurDate(String format) {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
String strDate = simpledateformat.format(calendar.getTime());
return strDate;
}
/**
* 返回TimeStamp对象
*
* @return
*/
public static Timestamp getCurrentTimestamp() {
Object obj = TypeCaseHelper.convert(getCurrentTime(), "Timestamp", "yyyy-MM-dd HH:mm:ss");
if (obj != null)
return (Timestamp) obj;
else
return null;
}
/**
* 将字符串型日期转换为日期型
*
* @param strDate
* 字符串型日期
* @param srcDateFormat
* 源日期格式
* @param dstDateFormat
* 目标日期格式
* @return Date 返回的util.Date型日期
*/
public static Date stringToDate(String strDate, String srcDateFormat, String dstDateFormat) {
Date rtDate = null;
Date tmpDate = (new SimpleDateFormat(srcDateFormat)).parse(strDate, new ParsePosition(0));
String tmpString = null;
if (tmpDate != null)
tmpString = (new SimpleDateFormat(dstDateFormat)).format(tmpDate);
if (tmpString != null)
rtDate = (new SimpleDateFormat(dstDateFormat)).parse(tmpString, new ParsePosition(0));
return rtDate;
}