Ⅰ 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+"秒";
}
}