時間java
1. java中輸入一個日期
public static void main(String[] args) throws ParseException {
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.print("請輸入:");
Date date = simpleDateFormat.parse(input.nextLine());
Calendar calendar=Calendar.getInstance();
calendar.setTime(date);
int week=calendar.get(Calendar.DAY_OF_WEEK);
if (week==1) {
//第幾個星期
int weekNum=calendar.get(Calendar.WEEK_OF_MONTH);
switch (weekNum) {
case 1:
System.err.println(1);
break;
case 2:
System.err.println(2);
break;
case 3:
System.err.println(3);
break;
case 4:
System.err.println(4);
break;
case 5:
System.err.println(5);
break;
}
}else{
System.err.println(0);
}
}
2. java如何獲取當前時間 年月日 時分秒
//得到long類型當前時間
longl=System.currentTimeMillis();
//new日期對
Datedate=newDate(l);
//轉換提日期輸出格式
SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-
ddHH:mm:ss");System.out.println(dateFormat.format(date));
(2)時間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);
}
}
3. java獲取時間
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) {
SimpleDateFormat sm=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sm.format(new Date()));
}
}
4. java中如何輸出當時的日期和時間
代碼如下:
packagecom.grace.test;
importjava.text.DateFormat;
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.Calendar;
importjava.util.Date;
publicclassshowDate{
publicstaticvoidmain(String[]args)throwsParseException{
Dated=newDate();
Strings=null;
/**輸出格式:MonMay0515:23:58CST2014*/
System.out.println(d);
/**輸出格式:2014-5-5*/
s=DateFormat.getDateInstance().format(d);
System.out.println(s);
/**輸出格式:2014-5-5*/
s=DateFormat.getDateInstance(DateFormat.DEFAULT).format(d);
System.out.println(s);
/**輸出格式:2014年5月5日星期一*/
s=DateFormat.getDateInstance(DateFormat.FULL).format(d);
System.out.println(s);
/**輸出格式:2014-5-5*/
s=DateFormat.getDateInstance(DateFormat.MEDIUM).format(d);
System.out.println(s);
/**輸出格式:14-5-5*/
s=DateFormat.getDateInstance(DateFormat.SHORT).format(d);
System.out.println(s);
/**輸出格式:2014-5-0500:00:00大寫H為24小時制*/
DateFormatsdf=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
s=sdf.format(d);
System.out.println(s);
/**輸出格式:2014-5-0500:00:00小寫h為12小時制*/
DateFormatsdf2=newSimpleDateFormat("yyyy-MM-ddhh:mm:ss");
s=sdf2.format(d);
System.out.println(s);
/**輸出格式:20140505000000*/
DateFormatsdf3=newSimpleDateFormat("yyyyMMddHHmmss");
s=sdf3.format(d);
System.out.println(s);
/**字元串轉換城日期格式*/
s=sdf.format(d);
Datetoday=sdf.parse(s);
System.out.println("字元串轉成日期1:"+today);
System.out.println("字元串轉成日期2:"+sdf.format(today));
/**單獨輸出年月日時分秒等*/
Calendarc=Calendar.getInstance();
System.out.println("年:"+c.get(Calendar.YEAR));
//月份從0開始,加1校正
System.out.println("月:"+(c.get(Calendar.MONTH)+1)+"");
System.out.println("日:"+c.get(Calendar.DAY_OF_MONTH));
System.out.println("時:"+c.get(Calendar.HOUR_OF_DAY));
System.out.println("分:"+c.get(Calendar.MINUTE));
System.out.println("秒:"+c.get(Calendar.SECOND));
System.out.println("當前時間毫秒數:"+c.getTimeInMillis());
System.out.println("當前時間:"+c.getTime());
}
}
5. java中的時間類型
public void compare(String str1,String str2){
try {
SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss");
Date date1 = s.parse(str1);
Date date2 = s.parse(str2);
if(date1.getTime()>=date2.getTime()){
System.out.println("str1>=str2");
}else
System.out.println("str1<str2");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
6. 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);
}
7. java中時間比較
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class TimeCompare {
public static int Hour(Date time){
SimpleDateFormat st=new SimpleDateFormat("yyyyMMddHH");
return Integer.parseInt(st.format(time));
}
public static Date StringToDate(String s){
Date time=new Date();
SimpleDateFormat sd=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try{
time=sd.parse(s);
}
catch (ParseException e) {
System.out.println("輸入的日期格式有誤!");
}
return time;
}
public static void main(String[] args) {
String a="2012/01/01 18:08:20";
String b="2012/01/01 18:01:20";
String c="2012/01/02 01:01:01";
if(Hour(StringToDate(a))<=Hour(StringToDate(b))&&Hour(StringToDate(a))<Hour(StringToDate(c)))
System.out.println("成功");
else
System.out.println("失敗");
}
}
已經debug通過,分給我吧
8. java 日期型加5分鍾
應該這樣寫
Calendar nowTime = Calendar.getInstance();
Date nowDate = (Date) nowTime.getTime();
Calendar afterTime = Calendar.getInstance();
afterTime.add(Calendar.MONTH, 5);
Date afterDate = (Date) afterTime.getTime();
我想說得是 java已經不推薦使用date類了,所以盡量用Calendar 來實現
9. java時間比較
用 JAVA Calendar,很簡潔
下面的例子供參考
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class myWeek
{
//獲得當前日期與其所在星期周一相差的天數
public static void main(String[] args)
{
String str="2014-07-08";
SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
try
{
Date mydate = formatDate.parse(str);
Calendar cal = Calendar.getInstance();
cal.setTime(mydate);
//獲得今天是一周的第幾天
//注意,一周是從星期日開始的,既dayOfWeek=1表示周日,ayOfWeek=2表示周一
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
if(dayOfWeek==1){System.out.println("與所在星期周一相差的天數:6");}
else
{
String strDatNum=String.valueOf(dayOfWeek-2);
System.out.println("與所在星期周一相差的天數:"+strDatNum);
}
}
catch (Exception e){}
}
}
10. java計算時間
可以這么寫:
publicstaticvoidcompute(){
Scannerscanner=newScanner(System.in);
inttime1,time2,hours,minutes;
time1=scanner.nextInt();
time2=scanner.nextInt();
Stringt1=String.valueOf(time1);
Stringt2=String.valueOf(time2);
//開始時間的小時
intt1_hour=Integer.parseInt(t1.substring(0,t1.length()-2));
//結束時間的小時
intt2_hour=Integer.parseInt(t2.substring(0,t2.length()-2));
//開始時間的分鍾
intt1_minute=Integer.parseInt(t1.substring(t1.length()-2));
//結束時間的分鍾
intt2_minute=Integer.parseInt(t2.substring(t2.length()-2));
//時間差的小時
hours=((t2_hour*60+t2_minute)-(t1_hour*60+t1_minute))/60;
//時間差的分鍾
minutes=((t2_hour*60+t2_minute)-(t1_hour*60+t1_minute))%60;
System.out.println("Thetrainjourneytimeis"+hours+"hrs"+minutes+"mins.");
scanner.close();
}