本文目录一览:
Java 如何把输入的时分秒转换再输出为秒数?
import java.util.Scanner;
public class Convert{
public static void main(String[] args){
System.out.println("请输入时分秒,格式为hh:mi:ss");
Scanner input=new Scanner(System.in);
String s=input.next();
int index1=s.indexOf(":");
int index2=s.indexOf(":",index1+1);
int hh=Integer.parseInt(s.substring(0,index1));
int mi=Integer.parseInt(s.substring(index1+1,index2));
int ss=Integer.parseInt(s.substring(index2+1));
System.out.println(hh*60*60+mi*60+ss);
}
}
java如何获取当前时间 年月日 时分秒
//得到long类型当前时间
long l = System.currentTimeMillis();
//new日期对
Date date = new Date(l);
//转换提日期输出格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-
dd HH:mm:ss");System.out.println(dateFormat.format(date));
扩展资料
package com.ob;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTest {
public static void main(String[] args) throws ParseException {
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));
System.out.println("秒: " + now.get(Calendar.SECOND));
System.out.println("当前时间毫秒数:" + now.getTimeInMillis());
System.out.println(now.getTime());
\t\tDate d = new Date();
System.out.println(d);
\t\tSimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
\t\tString dateNowStr = sdf.format(d);
System.out.println("格式化后的日期:" + dateNowStr);
\t\t
\t\tString str = "2012-1-13 17:26:33";
//要跟上面sdf定义的格式一样
\t\tDate today = sdf.parse(str);
System.out.println("字符串转成日期:" + today);
\t}
}
参考资料:Java - 百度百科
JAVA将时分秒格式的时间转化成秒数
public class TimeToSecond {
public static void main(String[] args) {
String time ="01:22:12";
String[] my =time.split(":");
int hour =Integer.parseInt(my[0]);
int min =Integer.parseInt(my[1]);
int sec =Integer.parseInt(my[2]);
int zong =hour*3600+min*60+sec;
System.out.println("共"+zong+"秒");
}
}
扩展资料
java将毫秒值转换为日期时间
public static void main(String[] args) {
long milliSecond = 1551798059000L;
Date date = new Date();
date.setTime(milliSecond);
System.out.println(new SimpleDateFormat().format(date));
}