‘壹’ java怎么把时间长转换成时分秒格式
小时:h=time/3600(整除)
分钟:m=(time-h*3600)/裤物60 (整除)
秒圆孝:s=(time-h*3600) mod 60 (取余)橘纯稿
‘贰’ Java中如何把日期转换为秒数
Date类有一个getTime()可以换回秒数,例如:
publicclassDateToSecond
{
publicstaticvoidmain(String[]args)
{
Datedate=newDate(System.currentTimeMillis());
System.out.println(date.getTime());
}
}
‘叁’ java中如何取系统时间精确到秒
1 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
2 Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int date = c.get(Calendar.DATE);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);
3 Date nowTime = new Date(System.currentTimeMillis());
SimpleDateFormat
sdFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String
retStrFormatNowDate = sdFormatter.format(nowTime);
‘肆’ java如何获取当前时间 年月日 时分秒
//得到long类型当前时间
longl=System.currentTimeMillis();
//new日期对
Datedate=newDate(l);
//转换提日期输出格式
SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-
ddHH:mm:ss");System.out.println(dateFormat.format(date));
(4)java时间转换成秒扩展阅读
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());
Date d = new Date();
System.out.println(d);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateNowStr = sdf.format(d);
System.out.println("格式化后的日期:" + dateNowStr);
String str = "2012-1-13 17:26:33";
//要跟上面sdf定义的格式一样
Date today = sdf.parse(str);
System.out.println("字符串转成日期:" + today);
}
}
‘伍’ 用java编写一个程序,键入一个以秒为单位的时间长度,然后换算成小时、分和秒的组合表达方式并输出结果。
publicclassTest{
privatestaticfinalintSECOND_PER_HOUR=60*60;
privatestaticfinalintSECOND_PER_MINUTE=60;
publicstaticvoidmain(String[]args){
longsecond=newScanner(System.in).nextLong();
longhour=second/SECOND_PER_HOUR;
second-=SECOND_PER_HOUR*hour;
intminute=(int)(second/SECOND_PER_MINUTE);
second-=SECOND_PER_MINUTE*minute;
System.out.println(String.format("%d小时%d分钟%d秒",hour,minute,second));
}
}
‘陆’ 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+"秒");
}
}
(6)java时间转换成秒扩展阅读
java将毫秒值转换为誉茄羡日期时间
public static void main(String[] args) {
long milliSecond = 1551798059000L;
Date date = new Date();
date.setTime(milliSecond);
System.out.println(new SimpleDateFormat().format(date));
}
‘柒’ 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 将时间转换成秒
public int returnSeconde(String instr){
String[] a=instr.splite("\\.");
String[] b=a[0].splite(":");
return Integer.valueOf(b[0])*60*60 + Integer.valueOf(b[1])*60 + Integer.valueOf(b[2]);
}
基本上就是这样了,没有调试过,也没做输入校验,不过基本思路没什么错。
‘玖’ java 毫秒转换时间
时间除以1000转换成秒,对60取余就是秒数,除以60后再对60取余是分,除以60后再对24取余是小时