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日期格式化
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test {
public static void main(String[] args) {
final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy年MM月dd日");
Calendar calendar = Calendar.getInstance();
Date date = new Date();
calendar.setTime(date);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(sdfInput.format(date) + " " + dayNames[dayOfWeek - 1]);
}
}
java日期格式化问题?
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currentTime = new Date();// 得到当前系统时间
String str_date = formatter.format(currentTime); // 将日期时间格式化
System.out.print(str_date );
这样肯定没问题啊
JAVA 中获取时间怎么格式化?
时间格式化输出主要有两种方式,代码如下:
//使用Calendar
Calendar now = Calendar.getInstance();
System.out.println("年:" + now.get(Calendar.YEAR));
System.out.println("月:" + (now.get(Calendar.MONTH) + 1));
System.out.println("日:" + now.get(Calendar.DAY_OF_MONTH));
System.out.println("时:" + now.get(Calendar.HOUR_OF_DAY));
System.out.println("分:" + now.get(Calendar.MINUTE));
ystem.out.println("秒:" + now.get(Calendar.SECOND));
//使用Date
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("当前时间:" + sdf.format(d));
扩展资料
JAVA中获取当前系统时间。
import java.util.Date;
import java.text.SimpleDateFormat;
public class NowString {
public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
}
}
参考资料来源:百度百科:Java