「java格式化时间字符串」java字符串转化为时间格式
今天给各位分享java格式化时间字符串的知识,其中也会对java字符串转化为时间格式进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
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);
}
}
java指定的字符串日期格式化及计算
package timeutil;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class TimeTest {
public static void main(String[] args) {
String s1 = "20151117190936";
String s2="20090915-17:20:12";
try {
Date date1 = new SimpleDateFormat("yyyyMMddHHmmss").parse(s1);
//输出格式
System.out.println("=======解析字符串1======");
System.out.println(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(date1));
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date1));
System.out.println("=======解析字符串2======");
Date date2 = new SimpleDateFormat("yyyyMMdd-HH:mm:ss").parse(s2);
System.out.println(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(date2));
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date2));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Java中怎么把字符串转换成日期格式
将字符串类型的时间转换成date类型可以使用SimpleDateFormat来转换,具体方法如下:
1、定义一个字符串类型的时间;
2、创建一个SimpleDateFormat对象并设置格式;
3、最后使用SimpleDateFormat的parse方法将String类型的时间转换成Date类型的时间。
具体代码如下:
String string = "2014-3-17";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = dateFormat.parse(string);
System.out.println(date.toLocaleString().split(" ")[0]);//切割掉不要的时分秒数据
} catch (ParseException e) {
e.printStackTrace();
}
介绍一下SimpleDateFormat:SimpleDateFormat 是 Java 中一个非常常用的类,该类用来对日期字符串进行解析和格式化输出。
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
望采纳。
在java里,什么是格式化字符串
格式化字符串就是按一定格式输出的字符串
举个例子来说,你想输出时间
2006-07-25
21:20:30
这就是一个格式化字符串,它的格式为
yyyy-mm-dd
hh:mm:ss
下面再给你一个操作的例子,比如说你要输出时间
formatdatetime('yyyy-mm-dd
hh:mm:ss',now);
就能够按我上面的格式输出时间了
又比如说你经过计算得到一个结果,你想用一个有格式的字符串输出
例如:计算结果是xxx,那么可以这样写:
format('计算结果是%d',[result]);
我给出的是delphi代码,如果你用c的,用printf语句也可以实现
printf("计算结果是%d",result);
关于java格式化时间字符串和java字符串转化为时间格式的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-09,除非注明,否则均为
原创文章,转载请注明出处。