JAVA字符串转日期或日期转字符串

JAVA字符串转日期或日期转字符串
文章中,用的API是SimpleDateFormat,它是属于java.text.SimpleDateFormat,所以请记得import进来。
用法: SimpleDateFormat sdf = new SimpleDateFormat( ” yyyy-MM-dd HH:mm:ss ” );

 这一行最重要,它确立了转换的格式,yyyy是完整的公元年,MM是月份,dd是日期,至于HH:mm:ss 就不需要我再解释了吧! 

PS:为什么有的格式大写,有的格式小写,那是怕避免混淆,例如MM是月份,mm是分;HH是24小时制,而hh是12小时制。

1.字符串转日期
 
2008-07-10 19:20:00 要把它转成日期,可以用 Date date = sdf.parse( ” 2008-07-10 19:20:00 ” );

2.日期转字符串

假如把今天的日期转成字符串可用 String str = sdf.format(new Date());
这个字符串内容的格式类似2008-07-10 19:20:00。
透过这个API我们便可以随心所欲的将日期转成我们想要的字符串格式,例如希望将日期输出成2008年7月10日,我们可以这么写:

    SimpleDateFormat sdf =   new SimpleDateFormat( " yyyy年MM月dd日 " ); 

    String str = sdf.format(new Date()); 

str便会依照我们设定的格式输出了。

附编写好的一个简单实例:

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class ConvertDemo {

/**
* 日期转换成字符串
* @param date
* @return str
*/
public static String DateToStr(Date date) {

SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
String str = format.format(date);
return str;
}

/**
* 字符串转换成日期
* @param str
* @return date
*/
public static Date StrToDate(String str) {

SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Date date = null;
try {
date = format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}

public static void main(String[] args) {

Date date = new Date();
System.out.println(“日期转字符串:” + ConvertDemo.DateToStr(date));
System.out.println(“字符串转日期:” + ConvertDemo.StrToDate(ConvertDemo.DateToStr(date)));

}

}

***************************** 下面的都是例子 *********************************************

Java中日期格式转换

/**
* 字符串转换为java.util.Date

* 支持格式为 yyyy.MM.dd G ‘at’ hh:mm:ss z 如 ‘2002-1-1 AD at 22:10:59 PSD’

* yy/MM/dd HH:mm:ss 如 ‘2002/1/1 17:55:00’

* yy/MM/dd HH:mm:ss pm 如 ‘2002/1/1 17:55:00 pm’

* yy-MM-dd HH:mm:ss 如 ‘2002-1-1 17:55:00’

* yy-MM-dd HH:mm:ss am 如 ‘2002-1-1 17:55:00 am’

* @param time String 字符串

* @return Date 日期

*/
public static Date stringToDate(String time){
SimpleDateFormat formatter;
int tempPos=time.indexOf(“AD”) ;
time=time.trim() ;
formatter = new SimpleDateFormat (“yyyy.MM.dd G ‘at’ hh:mm:ss z”);
if(tempPos>-1){
time=time.substring(0,tempPos)+
“公元”+time.substring(tempPos+”AD”.length());//china
formatter = new SimpleDateFormat (“yyyy.MM.dd G ‘at’ hh:mm:ss z”);
}
tempPos=time.indexOf(“-“);
if(tempPos>-1&&(time.indexOf(” “)<0)){
formatter = new SimpleDateFormat (“yyyyMMddHHmmssZ”);
}
else if((time.indexOf(“/”)>-1) &&(time.indexOf(” “)>-1)){
formatter = new SimpleDateFormat (“yyyy/MM/dd HH:mm:ss”);
}
else if((time.indexOf(“-“)>-1) &&(time.indexOf(” “)>-1)){
formatter = new SimpleDateFormat (“yyyy-MM-dd HH:mm:ss”);
}
else if((time.indexOf(“/”)>-1) &&(time.indexOf(“am”)>-1) ||(time.indexOf(“pm”)>-1)){
formatter = new SimpleDateFormat (“yyyy-MM-dd KK:mm:ss a”);
}
else if((time.indexOf(“-“)>-1) &&(time.indexOf(“am”)>-1) ||(time.indexOf(“pm”)>-1)){
formatter = new SimpleDateFormat (“yyyy-MM-dd KK:mm:ss a”);
}
ParsePosition pos = new ParsePosition(0);
java.util.Date ctime = formatter.parse(time, pos);

return ctime; 

}

/**
* 将java.util.Date 格式转换为字符串格式’yyyy-MM-dd HH:mm:ss’(24小时制)

* 如Sat May 11 17:24:21 CST 2002 to ‘2002-05-11 17:24:21’

* @param time Date 日期

* @return String 字符串

*/

public static String dateToString(Date time){
SimpleDateFormat formatter;
formatter = new SimpleDateFormat (“yyyy-MM-dd HH:mm:ss”);
String ctime = formatter.format(time);

return ctime; 

}

/**
* 将java.util.Date 格式转换为字符串格式’yyyy-MM-dd HH:mm:ss a’(12小时制)

* 如Sat May 11 17:23:22 CST 2002 to ‘2002-05-11 05:23:22 下午’

* @param time Date 日期

* @param x int 任意整数如:1

* @return String 字符串

*/
public static String dateToString(Date time,int x){
SimpleDateFormat formatter;
formatter = new SimpleDateFormat (“yyyy-MM-dd KK:mm:ss a”);
String ctime = formatter.format(time);

return ctime; 

}

/**
*取系统当前时间:返回只值为如下形式
*2002-10-30 20:24:39
* @return String
*/
public static String Now(){
return dateToString(new Date());
}

/**
*取系统当前时间:返回只值为如下形式
*2002-10-30 08:28:56 下午
*@param hour 为任意整数
*@return String
*/
public static String Now(int hour){
return dateToString(new Date(),hour);
}

/**
*取系统当前时间:返回值为如下形式
*2002-10-30
*@return String
*/
public static String getYYYY_MM_DD(){
return dateToString(new Date()).substring(0,10);

}

/**
*取系统给定时间:返回值为如下形式
*2002-10-30
*@return String
*/
public static String getYYYY_MM_DD(String date){
return date.substring(0,10);

}

public static String getHour(){
SimpleDateFormat formatter;
formatter = new SimpleDateFormat (“H”);
String ctime = formatter.format(new Date());
return ctime;
}

public static String getDay(){
SimpleDateFormat formatter;
formatter = new SimpleDateFormat (“d”);
String ctime = formatter.format(new Date());
return ctime;
}

public static String getMonth(){
SimpleDateFormat formatter;
formatter = new SimpleDateFormat (“M”);
String ctime = formatter.format(new Date());
return ctime;
}

public static String getYear(){
SimpleDateFormat formatter;
formatter = new SimpleDateFormat (“yyyy”);
String ctime = formatter.format(new Date());
return ctime;
}

public static String getWeek(){
SimpleDateFormat formatter;
formatter = new SimpleDateFormat (“E”);
String ctime = formatter.format(new Date());
return ctime;
}

在jsp页面中的日期格式和sqlserver中的日期格式不一样,怎样统一?

在页面上显示输出时,用下面的函数处理一下

public class DateUtil(){
public static String fmtShortEnu(Date myDate) {
SimpleDateFormat formatter = new SimpleDateFormat(“yyyy/MM/dd”);
String strDate = formatter.format(myDate);
return strDate;
}
}

new java.text.SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
new java.text.SimpleDateFormat(“yyyy-MM-dd”)
建议还是把sqlserver的字段类型改成varchar的吧,用字符串处理可以完全按照自己的意愿处理,没有特殊的需求,不要使用date型

字串日期格式转换
用的API是SimpleDateFormat,它是属於java.text.SimpleDateFormat,所以请记得import进来!

用法:
SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
这一行最重要,它确立了转换的格式,yyyy是完整的西元年,MM是月份,dd是日期, 至於HH:mm:ss就不需要我再解释了吧!
ps:为什麽有的格式大写,有的格式小写,那是怕避免混淆,例如MM是月份,mm是分;HH是24小时制,而hh是12小时制

1.字串转日期:
 2002-10-8 15:30:22要把它转成日期,可以用
 Date date=sdf.parse(“2002-10-8 15:30:22”);
2.日期转字串
 假如把今天的日期转成字串可用
 String datestr=sdf.format(new Date());
 这个字串的内容便类似2002-10-08 14:55:38

透过这个API我们便可以随心所欲的将日期转成我们想要的字串格式,例如希望将日期输出成2002年10月08日,
我们可以这麽写:
SimpleDateFormat sdf=new SimpleDateFormat(“yyyy年MM月dd日”);
String datestr=sdf.format(new Date());
datestr便会依照我们设定的格式输出

//对日期格式的转换成(”yyyy-MM-dd”)格式的方法
public java.sql.Date Convert(String str)
{
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(“yyyy-MM-dd”);
try
{
java.util.Date d = sdf.parse(str);
java.sql.Date d1 = new java.sql.Date(d.getTime());
return d1;
}
catch(Exception ex)
{
ex.printStackTrace();
return null;
}
}
应用如下:
ctmt.setDate(7,this.Convert(info.getManBirth())); // @DATETIME

常用日期问题集锦

1、获取服务器端当前日期:
<%@ page import=”java.util.Date”%>
<%
Date myDate = new Date();
%>

2、获取当前年、月、日:
<%@ page import=”java.util.Date”%>

}

小例2:
如何获取当前时间?
String now=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”).format(Calendar.getInstance().getTime());
Date createDate=convertDate(now); //转换成Date型
//将字符串转换成日期型的方法;
public Date convertDate(String planedDate){
Date date=new Date();
SimpleDateFormat dd=new SimpleDateFormat(“yyyy-MM-dd”);
String d=dd.format(new Date());
System.out.print(d);
try {
date=dd.parse(planedDate);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
return date;

}

小例3:
如何获得系统的year,month,day?
Calendar c = Calendar.getInstance();
c.setTime(new java.util.Date());
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

int second = c.get(Calendar.SECOND);

小例4:
JAVA中获得本地系统时间的方法
import java.util.*;

public class D 

{
public static void main(String []abc)
{
int y,m,d,h,mi,s;
Calendar cal=Calendar.getInstance();
y=cal.get(Calendar.YEAR);
m=cal.get(Calendar.MONTH);
d=cal.get(Calendar.DATE);
h=cal.get(Calendar.HOUR_OF_DAY);
mi=cal.get(Calendar.MINUTE);
s=cal.get(Calendar.SECOND);
System.out.println(“现在时刻是”+y+”年”+m+”月”+d+”日”+h+”时”+mi+”分”+s+”秒”);
}
}
###########################################################
public class Main{
public static void main(String[] args)
{
java.util.Calendar c=java.util.Calendar.getInstance();
java.text.SimpleDateFormat f=new java.text.SimpleDateFormat(“yyyy年MM月dd日hh时mm分ss秒”);
System.out.println(f.format(c.getTime()));
}

}

小例5:
1>先导入两个类java.util.; java.text.;
2>加简单的一句话:
String date=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”).format(Calendar.getInstance).getTime());
date就是你得到的时间,显示格式可以根据(“yyyy-MM-dd HH:mm:ss”)的格式随意调整


小例6:
在JSP页面上可以通过一下方法获取:
function printDate()
{
var today=new Date();
var day=today.getDate();
var month=today.getMonth()+1;
var year=today.getFullYear().toString();
var box1=document.stats.intoDate;
if((month/10)<1)
{
month=(“0”+month).toString();
}
if((day/10)<1)
{
day=(“0”+today.getDate()).toString();
}
box1.value=year+”-“+month+”-“+day;
}

注:文章转载自http://blog.csdn.net/cwcwj3069/article/details/52164559

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值