时间格式是date,2018/3/17 12:12:00
我要把他变成显示为 12:12
请问各位大神,这个要怎么实现?
public static void main(String[] args){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(new Date()));
SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
System.out.println(sdf2.format(new Date()));
}
自己去eclipse或者ieda上跑一下。如果解决了你的问题,请及时采纳。
如果你只想要显示时分,那么下面这段就直接可以:
String time = "2018/3/17 12:12:00".replaceAll("^.+\s+(\d{2}:\d{2}).*$", "$1");
System.out.println(time);
public class TestDate {
public static void main(String[] args) throws ParseException {
Date nowTime = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String change = sdf.format(nowTime);
System.out.println(change);
Date dd = sdf.parse(change);
SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
String change2 = sdf2.format(dd);
System.err.println(change2);
}
}
第二行的字符串中,把要复制的字符串放进去,然后把每一位的字母,年换成y 月换成M 日换成d 时换成H 分换成m 秒换成s 其他的字符不要动
public static void main(String[] args){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/M/d HH:mm:ss");
System.out.println(sdf.format(new Date()));
SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
System.out.println(sdf2.format(new Date()));
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/M/d HH:mm:ss");
Date date= null;
try {
date = sdf.parse("2018/03/17 12:12:00");
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
System.out.print(sdf2.format(date));
注意冒号采用英文输入发输入
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
System.out.println(simpleDateFormat.format(date));
public static void main(String[] args){
String date="2018/3/17 12:12:00";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
try {
System.out.println(sdf2.format(sdf1.parse(date)));
} catch (ParseException e) {
e.printStackTrace();
}
}
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
String date = dateFormat.format(new Date());
把你要 new Date() 换成你要格式化的 时间格式