Ⅰ java如何获取当前时间 年月日 时分秒
//得到long类型当前时间
longl=System.currentTimeMillis();
//new日期对
Datedate=newDate(l);
//转换提日期输出格式
SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-
ddHH:mm:ss");System.out.println(dateFormat.format(date));
(1)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怎么把时间长(秒)转换成时分秒格式
小时:h=time/3600(整除)
分钟:m=(time-h*3600)/60 (整除)
秒:s=(time-h*3600) mod 60 (取余)
Ⅲ 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+"秒");
}
}
(3)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如何把时间格式转为毫秒
获取毫秒数,即long类型的数值,仅能返回自 1970 年 1 月 1 日 00:00:00 GMT 以来的毫秒数。
一楼、二楼的回答就是正确的,不过在使用中还需要根据自身使用环境,直接使用或者进一步按需优化后再使用。
最常使用的就是,把String类型的日期先转换为Date类型,最后直接调用.getTime()即可,这也是比较方便的了。
还有就是以上提到的Timestamp类中的valueOf(String s) 方法,这里一定要注意,给定的字符串日期型数据必须符合置顶指定格式:yyyy-mm-dd hh:mm:ss[.fffffffff],否则会抛出异常。
PS>
Ⅶ java时间转换
java中毫秒转日期:
//毫秒转换为日期
public static void main(String[] args) {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
long now = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(now);
System.out.println(now + " = " + formatter.format(calendar.getTime()));
// 日期转换为毫秒 两个日期想减得到天数
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String start="2011-09-20 12:30:45";
String end ="2011-10-20 6:30:00";
//得到毫秒数
long timeStart=sdf.parse(start).getTime();
long timeEnd =sdf.parse(end).getTime();
//两个日期想减得到天数
long dayCount= (timeEnd-timeStart)/(24*3600*1000);
System.out.println(dayCount);
}
Ⅷ 用java怎样把一个以秒为单位的数转换成时分秒,比如说3670s表示为:1时1分10秒 详细的答案给全分!
我刚写的你看看把
public class h
{
public static void main(String args[]){
System.out.print( cal(3670));
}
public static String cal(int second){
int h = 0;
int d = 0;
int s = 0;
int temp = second%3600;
if(second>3600){
h= second/3600;
if(temp!=0){
if(temp>60){
d = temp/60;
if(temp%60!=0){
s = temp%60;
}
}else{
s = temp;
}
}
}else{
d = second/60;
if(second%60!=0){
s = second%60;
}
}
return h+"时"+d+"分"+s+"秒";
}
}