
正文
格式化日期java代码 java 日期格式化为yyyymmdd mmss
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java将时间格式化(急)
1、通过MessageFormat转化
String dateTime = MessageFormat.format("{0,date,yyyy-MM-dd-HH-mm:ss:ms}" ,
new Object[] {
new java.sql.Date(System.currentTimeMillis())
});
说明: yyyy-MM-dd-HH-mm:ss:ms 年yyyy 月MM 日dd 时(大写为24进制,小写为12进制) 分mm 秒ss 微妙ms。
2、通过SimpleDateFormat 转化
SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //格式化当前系统日期
String dateTime = dateFm.format(new java.util.Date());
相关问答
Q1: Java时间格式化
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
class SimpleDateFormat01
{
public static void main(String[] args) throws ParseException
{
String str = "Fri Oct 27 15:38:07 CST 2017";
SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf1.parse(str);//提取格式中的日期
String newStr = sdf2.format(date); //改变格式
System.out.println("转换之后的日期:"+newStr);
}
}
Q2: java 日期格式化
你真会开玩笑,日期型就是日期型的,没有几种格式可以转换(比如格林威治时间,距历元所经过的毫秒,也有用long型来表示的,都是计算机中的一种表示而已),根本无法转换成你要的这种格式,真的要格式不过是把日期打印出来的时候的格式而已,也就是String的格式而已!
而且我感觉没有比较关注日期类型的具体类型,你要String的类型就好了:
下面是一个简单的例子:
SimpleDateFormat格式化日期
import java.text.SimpleDateFormat;
import java.util.Date;
public class test {
public static void main(String []aa){
SimpleDateFormat dateformat1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
String a1=dateformat1.format(new Date());
System.out.println("时间2:"+a1);
System.out.println(new Date().getYear()+1900);
SimpleDateFormat dateformat2=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");
String a2=dateformat2.format(new Date());
System.out.println("时间2:"+a2);
}
}
Q3: 日期格式化java程序代码
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;
public class Cat {
public static void main(String[] args) {
System.out.print("Please input a date in format yyyy-MM-dd HH:mm:ss: ");
String input = new Scanner(System.in).nextLine().trim();
try {
Calendar cal = Calendar.getInstance();
cal.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(input));
cal.add(Calendar.HOUR, 100);
String date = new SimpleDateFormat("HH:mm:SS yyyy/MM/dd").format(cal.getTime());
System.out.println(date);
} catch (ParseException e) {
System.out.println("日期时间不正确");
}
}
}
------------------测试
Please input a date in format yyyy-MM-dd HH:mm:ss: 2011-6-30 3:10:10
07:10:00 2011/07/04
Q4: Java格式化字符串日期
package test;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateStyle {
public static void main(String[] args) {
SimpleDateFormat dateformat1 = new SimpleDateFormat(
"yyyyMMddHHmmss");
Date date=new Date();
date.setYear(2014-1900);
date.setMonth(3-1);
date.setDate(29);
date.setHours(19);
date.setMinutes(13);
date.setSeconds(0);
String a1 = dateformat1.format(date);
System.out.println("时间2:" + a1);
}
}
效果:
时间2:20140329191300
希望能帮到你。
或者因为那个方法都过时了,你可以用Calendar的方法,代码如下:
package test;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateStyle {
public static void main(String[] args) {
SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyyMMddHHmmss");
Calendar c = dateformat1.getCalendar();
c.set(2013, 14, 29, 19, 13, 0);
Date d = c.getTime();
System.out.println(d);
String a1 = dateformat1.format(d);
System.out.println("时间2:" + a1);
}
}
效果:
Sat Mar 29 19:13:00 CST 2014
时间2:20140329191300
望采纳。
Q5: java中怎么格式化日期??
你可以用String类的format方法,例如: System.out.println(String.format("%ty年%tm月%td日",date));下面是一个完整的例子。
public class FormatDateTest
{
public static void main(String[] args)
{
Date date = new Date(System.currentTimeMillis());
System.out.println(String.format("%ty年%tm月%td日",date));
System.out.println(String.format("%tY年%tm月%td日",date));
System.out.println(String.format("%tY年%tm月%td日%tH时%tM分%tS秒",date));
}
}
%ty是格式化年,%tm是格式化年,%td是格式化天,%tH格式化发时,%tM格式化分,%tS格式化秒。另外%tY是把年格式化为四位形式,如1999,而不是99。%tI是把时格式化为12小时制。格式化字符串中的是表示格式化同一个日期,当然你也可以这么写: System.out.println(String.format("%ty年%tm月%td日",date,date,date));
关于格式化日期java代码和java 日期格式化为yyyymmdd mmss的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






