① java 給固定日期(字元串)加上時分秒
如果你用的是 Java8:
importjava.time.Duration;
importjava.time.LocalTime;
publicclassTest{
=LocalTime.of(0,0,0);
publicstaticvoidmain(String[]args)throwsException{
LocalTimetime=LocalTime.parse("21:53:00");
LocalTimeaugment=LocalTime.parse("01:50:22");
LocalTimetime2=plusTime(time,augment);
System.out.println("time2:"+time2);
}
/**
*在current的基礎上增加augment所表示的時間(間隔)
*/
(LocalTimecurrent,LocalTimeaugment){
Durationration=Duration.between(START,augment);
returncurrent.plus(ration);
}
}
運行:
② JAVA 時間相加
如果要取出
int getDate()
已過時。 從 JDK 1.1 開始,由 Calendar.get(Calendar.DAY_OF_MONTH) 取代。
int getDay()
已過時。 從 JDK 1.1 開始,由 Calendar.get(Calendar.DAY_OF_WEEK) 取代。
int getHours()
已過時。 從 JDK 1.1 開始,由 Calendar.get(Calendar.HOUR_OF_DAY) 取代。
int getMinutes()
已過時。 從 JDK 1.1 開始,由 Calendar.get(Calendar.MINUTE) 取代。
int getMonth()
已過時。 從 JDK 1.1 開始,由 Calendar.get(Calendar.MONTH) 取代。
int getSeconds()
已過時。 從 JDK 1.1 開始,由 Calendar.get(Calendar.SECOND) 取代。
long getTime()
返回自 1970 年 1 月 1 日 00:00:00 GMT 以來此 Date 對象表示的毫秒數。
如果要相加
void setDate(int date)
已過時。 從 JDK 1.1 開始,由 Calendar.set(Calendar.DAY_OF_MONTH, int date) 取代。
void setHours(int hours)
已過時。 從 JDK 1.1 開始,由 Calendar.set(Calendar.HOUR_OF_DAY, int hours) 取代。
void setMinutes(int minutes)
已過時。 從 JDK 1.1 開始,由 Calendar.set(Calendar.MINUTE, int minutes) 取代。
void setMonth(int month)
已過時。 從 JDK 1.1 開始,由 Calendar.set(Calendar.MONTH, int month) 取代。
void setSeconds(int seconds)
已過時。 從 JDK 1.1 開始,由 Calendar.set(Calendar.SECOND, int seconds) 取代。
void setTime(long time)
設置此 Date 對象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以後 time 毫秒的時間點。
void setYear(int year)
已過時。 從 JDK 1.1 開始,由 Calendar.set(Calendar.YEAR, year + 1900) 取代。
最好用Calendar類代替,更方便
③ Java生成大量按鈕時如何做到瞬間載入
你可以用多線程來控制,等所有的按鈕在後端載入後,最後那個顯示的線程再進行
④ java 每秒運算
這個很簡單啊,我寫的這個你看下
public class Test
{
public static void main(String args[])
{
int sum=0;
for(int i=1;i<=10;i++)
{
sum=sum+i;
try
{
Thread.sleep(1000);//1000ms是一秒,就是讓cpu停止1秒鍾,時間可以自己設定
}
catch(Exception e){e.printStackTrace();}
System.out.println("sum="+sum);
}
}
}
對於你補充:
不需要導入其他的東西,但是有一點需要注意,就是sleep可能會拋出異常 ,所以要進行異常處理
對於影響來說,主線程里的程序肯定會有影響 ,因為它停的是主線程
但是對於其它的線程沒有影響
⑤ java 取系統時間並加1秒鍾
packagetest;
importjava.util.Calendar;
publicclassJButtonTest
{
publicstaticvoidmain(String[]args)
{
Calendarcalendar=Calendar.getInstance();
System.out.println(calendar.getTime());
calendar.add(Calendar.SECOND,1);
System.out.println(calendar.getTime());
}
}
⑥ java 時間相加問題
publicclassClock{
privateinthour;
privateintmin;
privateintsecond;
publicClock(inthour,intmin,intsecond){
this.hour=hour;
this.min=min;
this.second=second;
}
publicvoidadd(Clockclock){
intse=this.getSecond()+clock.getSecond();
intmin=this.getMin()+clock.getMin();
inthour=this.getHour()+clock.getHour();
while(se>=60){//如果秒數滿60則-60秒,加一分鍾
se=se-60;
min=min+1;
}
while(min>=60){
min=min-60;
hour=hour+1;
}
while(hour>=24){
hour=hour-24;
}
this.setSecond(se);
this.setMin(min);
this.setHour(hour);
}
publicintgetHour(){
returnhour;
}
publicvoidsetHour(inthour){
this.hour=hour;
}
publicintgetMin(){
returnmin;
}
publicvoidsetMin(intmin){
this.min=min;
}
publicintgetSecond(){
returnsecond;
}
publicvoidsetSecond(intsecond){
this.second=second;
}
publicStringformatedTime(){
return(this.getHour()>10?this.getHour():"0"+this.getHour())
+":"
+(this.getMin()>10?this.getMin():"0"+this.getMin())
+":"
+(this.getSecond()>10?this.getSecond():"0"
+this.getSecond());
}
publicstaticvoidmain(String[]args){
Clockclock=newClock(19,59,59);
ClockanotherClock=newClock(0,0,1);
clock.add(anotherClock);
System.out.println(clock.formatedTime());
}
}
//其中三目運算只是為了保證小於10的前面加個0,符合時間顯示規范
⑦ JAVA如何給原來的時間加上個毫秒
2010-01-10 19:13:31.XXX
用字元串來做就行了,只要格式和上面的一樣就可以了
⑧ java 兩個時間段計算
兩個時間段四個時間點,相當於時間軸上的兩條線段(b代表起點,e代表端點,b<=e)和4個端點。
可分3種情況:
1.不相交。(b1-----e1)【b2-----e2】(b1-----e1)。if(e1<b2||b1>e2)此時,重合天數為零。
2.相交。
情況一:(b1---【b2---e1)----e2】 if(b1<b2&&e1<e2&&e1>b2)
情況二:【b2---(b1---e2】----e1) if(b1>b2&&b1<e2&&e2<e1)
3.包含:計算較短的時間段日期長度。
(b1---【b2-----e2】--e1) if(b1<b2&&e1>e2)
【b2---(b1-----e1)--e2】 if(b1>b2&&e1<e2)
實現代碼如下:
[java] view plain
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* @author skysnow
*
*/
public class myDateUtil {
/**
*這里共有2個時間段(b1-----e1)【b2-----e2】,4個時間點;
*相當於兩條線段(b代表起點,e代表端點,b<=e),4個端點。
*可分3種情況:
*1.不相交。(b1-----e1)【b2-----e2】(b1-----e1)。if(e1<b2||b1>e2)此時,重合天數為零。
*2.相交。
*情況一:(b1---【b2---e1)----e2】 if(b1<b2&&e1<e2&&e1>b2)
*情況二:【b2---(b1---e2】----e1) if(b1>b2&&b1<e2&&e2<e1)
*3.包含:計算較短的時間段日期長度。
*(b1---【b2-----e2】--e1) if(b1<b2&&e1>e2)
*【b2---(b1-----e1)--e2】 if(b1>b2&&e1<e2)
* @param begindate1 開始日期
* @param enddate1 結束日期
* @param begindate2開始日期
* @param enddate2 結束日期
* @return
*/
public static String getDayCoincidence(Date begindate1,Date enddate1,Date begindate2,Date enddate2){
long b1=begindate1.getTime();
long e1=enddate1.getTime();
long b2=begindate2.getTime();
long e2=enddate2.getTime();
assert(b1<e1&&b2<e2);
String coincidenceday;
if(b1<=b2&&e1>=e2){//(b1---【b2-----e2】--e1)
System.out.println("1包含2");
coincidenceday=getDayDifference(enddate2,begindate2);
}else if(b1>=b2&&e1<=e2){//【b2---(b1-----e1)--e2】
System.out.println("2包含1");
coincidenceday=getDayDifference(enddate1,begindate1);
}else if(b1>=b2&&b1<=e2&&e2<=e1){//【b2---(b1---e2】----e1)
System.out.println("相交");
coincidenceday=getDayDifference(enddate2,begindate1);
}else if(b1<=b2&&e1<=e2&&e1>=b2){//(b1---【b2---e1)----e2】
System.out.println("相交");
coincidenceday=getDayDifference(enddate1,begindate2);
}else if(e1<=b2||b1>=e2){
coincidenceday="0";
}else{
coincidenceday="";
System.out.println("意料外的日期組合,無法計算重合天數!");
}
System.out.println("重合天數為["+coincidenceday+"]天。");
return coincidenceday;
}
/**
* 計算兩個日期的相差天數(d1-d2)
* @param d1
* @param d2
* @return
*/
public static String getDayDifference(Date d1,Date d2){
StringBuffer ds = new StringBuffer();
try{
long num = (d1.getTime()-d2.getTime())/1000;
long days = num/(3600*24);
if(days>=0)ds.append(days);
}catch(Exception e){
ds=new StringBuffer("");
e.printStackTrace();
}
return ds.toString();
}
public static Date stringToDate(String strDate) {
if (strDate==null){return null;}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
public static String getThisMonth()
{
// 本月的第一天
Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.DATE, 1);
SimpleDateFormat simpleFormate = new SimpleDateFormat("yyyy-MM-dd");
String fd = simpleFormate.format(calendar.getTime());
// 本月的最後一天
calendar.set( Calendar.DATE, 1 );
calendar.roll(Calendar.DATE, - 1 );
String ld = simpleFormate.format(calendar.getTime());
return fd+","+ld;
}
public static void main(String[] args) {
String[] thisMonth=getThisMonth().split(",");
Date begindate1 = stringToDate(thisMonth[0]+" 00:05:00");
Date enddate1 = stringToDate(thisMonth[0]+" 24:05:00");;
Date begindate2 = stringToDate(thisMonth[0]+" 00:05:00");
Date enddate2 = stringToDate(thisMonth[1]+" 00:00:00");
System.out.println(getDayCoincidence(begindate1, enddate1, begindate2, enddate2));
}
}
⑨ 用JAVA怎麼使變數每秒鍾加1急!!
其實就是讓線程休眠一秒就可以了很簡單
例子如下
package com.zhh.test;
public class Count {
public static void main(String[] args){
int count = 0;
while(true){
System.out.println(count++);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
⑩ 在此java程序上,添加實現一個方法add(),增加一秒後,並顯示時間。
class Clock{
int hour,min,sec;
Clock(int h,int m,int s){
hour=h;
min=m;
sec=s;
}
Clock(){}
void add(){
sec++;
}
void show()
{
System.out.printf("時間 %d:%d:%d",hour,min,sec);
System.out.println();
}
}
public class TextClass{
public static void main(String args[]){
Clock c1=new Clock(4,20,9);
Clock c2=new Clock(2,3,4);
Clock c3=new Clock(14,2,9);
c1.show();
c2.show();
c3.show();
c3.add();
c3.show();
}
}