java 日期格式化 现在有yyyymmddhhmmss格式的日期我想格式化成yyyy/mm/dd hh:mm和yyyy/mm/dd如果传入第一种的格式的就格式化成第一种如果传入的参数是第二种的话,格式成第二种谢谢大家 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 format(yyyy/mm/dd hh:mm)format(yyyy/mm/dd )就可以 了 传入参数不知道能不能根据长度判断import java.sql.Timestamp;import java.text.SimpleDateFormat;import java.util.Calendar;public class MyTest1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub MyTest1 test = new MyTest1(); Calendar cal = test.stringToCal("2009080415525", "yyyyMMddHHmmss"); String str = test.calToString(cal, "yyyy-MM-dd HH:mm:ss"); System.out.println(str); } /** * 将字符串日期转换成Calendar型 * @param datestring * @param format * @return */ public static Calendar stringToCal(String datestring,String format) { Calendar cal = Calendar.getInstance(); SimpleDateFormat df = new SimpleDateFormat ( format ); try { if (datestring!=null && datestring.length()!=0) cal.setTime(new Timestamp(df.parse(datestring).getTime())); } catch (Exception ex) { ex.printStackTrace(); cal =null; } return cal; } /** * 将Calendar型转换为String型,指定日期格式输出 * @param source_cal * @param format * @return */ public static String calToString(Calendar source_cal,String format) { String dateString = null; try{ if (source_cal!=null){ SimpleDateFormat sdf = new SimpleDateFormat (format); sdf.setCalendar(source_cal); dateString = sdf.format(source_cal.getTime()); } }catch (Exception ex) { System.out.println(ex.getMessage()); dateString = null; } return dateString; }} 1、先转为日期Calendar,再用DateFormat进行格式化输出2、直接拆分字符串 使用此方法:SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm");import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;public class TestTime {/*** @param args*/public static void main(String[] args) { String s1 = "yyyyMMddHH"; String s2 = "2008080808"; String s3 = "2008080807"; TestTime tt = new TestTime(); tt.timeop(s2,s1,s3);}//将日期转换为毫秒数public long timeop(String s1,String pattern,String s3) { // 传入的参数要与yyyyMMddHH的格式相同 "yyyyMMddHH" SimpleDateFormat simpledateformat = new SimpleDateFormat(pattern); Date date2 = null; Date date3 = null; try { date2 = simpledateformat.parse(s1); date3 = simpledateformat.parse(s3); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); }// 将参数按照给定的格式解析参数 System.out.println(date2.getTime()); System.out.println(date3.getTime()); System.out.println(date2.getTime() - date3.getTime()); return date2.getTime();}}Date dd = new Date(m) ;//通过毫秒数构造Date对象// 时间格式设置 SimpleDateFormat dat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"); Date d = new Date(); System.out.println(d.toString() + " *** " + dat.format(d)); 补充说明,传入的是字符串就是根据传入字符串的格式进行格式化回二楼的我要的是yyyy/MM/dd HH 只是这中形式的话,能够用那个函数吗我是了一下,好像出错 判断值,我想你应该会吧,可以在值传入的时候就判断if(str.length()==8){...}if(str.length()==10){...}so,so. 2楼基本已经写出来了,就用java.text.SimpleDateFormat中的构造方法就可以实现,具体建议你去看看API 分别生成两个SimpleDateFormat对象就可以了 页面跳转时图片路径出现的问题 请教高手一个快逸报表的初级问题 在不同浏览器中要实现多媒体播放的问题 [急]用JSP+Mysql开发网站的费用? jb2005 debug的时候怎么看一个对象里面的成员变量 请问大家一个关于option属性的问题 Frame框架中的页面如何得到Request中的属性 高手请进-----如何实现每隔50秒访问一次数据库的一张表?(Thread 可以吗) 用soapUI调用linux上weblogic发布的webservice报错 请问如何用javamail收发附件? STRUTS 中action执行时间长,页面等待问题 如何抽象这个方法为公共方法?
format(yyyy/mm/dd )就可以 了
import java.text.SimpleDateFormat;
import java.util.Calendar;public class MyTest1 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyTest1 test = new MyTest1();
Calendar cal = test.stringToCal("2009080415525", "yyyyMMddHHmmss");
String str = test.calToString(cal, "yyyy-MM-dd HH:mm:ss");
System.out.println(str);
} /**
* 将字符串日期转换成Calendar型
* @param datestring
* @param format
* @return
*/
public static Calendar stringToCal(String datestring,String format)
{
Calendar cal = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat ( format );
try
{
if (datestring!=null && datestring.length()!=0)
cal.setTime(new Timestamp(df.parse(datestring).getTime()));
}
catch (Exception ex)
{
ex.printStackTrace();
cal =null;
}
return cal;
}
/**
* 将Calendar型转换为String型,指定日期格式输出
* @param source_cal
* @param format
* @return
*/
public static String calToString(Calendar source_cal,String format)
{
String dateString = null; try{
if (source_cal!=null){
SimpleDateFormat sdf = new SimpleDateFormat (format);
sdf.setCalendar(source_cal);
dateString = sdf.format(source_cal.getTime());
}
}catch (Exception ex)
{
System.out.println(ex.getMessage());
dateString = null;
}
return dateString;
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class TestTime {/**
* @param args
*/
public static void main(String[] args) {
String s1 = "yyyyMMddHH";
String s2 = "2008080808";
String s3 = "2008080807";
TestTime tt = new TestTime();
tt.timeop(s2,s1,s3);
}
//将日期转换为毫秒数
public long timeop(String s1,String pattern,String s3) {
// 传入的参数要与yyyyMMddHH的格式相同 "yyyyMMddHH"
SimpleDateFormat simpledateformat = new SimpleDateFormat(pattern);
Date date2 = null;
Date date3 = null; try {
date2 = simpledateformat.parse(s1);
date3 = simpledateformat.parse(s3);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// 将参数按照给定的格式解析参数
System.out.println(date2.getTime());
System.out.println(date3.getTime());
System.out.println(date2.getTime() - date3.getTime());
return date2.getTime();
}}Date dd = new Date(m) ;//通过毫秒数构造Date对象
// 时间格式设置
SimpleDateFormat dat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
Date d = new Date(); System.out.println(d.toString() + " *** " + dat.format(d));
就是根据传入字符串的格式进行格式化
回二楼的
我要的是yyyy/MM/dd HH 只是这中形式的话,能够用那个函数吗
我是了一下,好像出错
if(str.length()==8){
...
}if(str.length()==10){
...
}so,so.