導航:首頁 > 編程語言 > javaurl時間

javaurl時間

發布時間:2023-08-17 01:43:52

1. 在java中怎麼獲取北京時間各位不要吝嗇!

//這是通過網路獲取北京時間的方法
java.util.Locale locale=java.util.Locale.CHINA; //這是獲得本地中國時區

String pattern = "yyyy-MM-dd kk:mm:ss zZ";//這是日期格式
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat(pattern,locale);//設定日期格式
java.util.Date date = new java.util.Date();
java.net.URL url=new URL("http://www.bjtime.cn");//取得資源對象
java.net.URLConnection uc=url.openConnection();//生成連接對象
uc.connect(); //發出連接
long ld=uc.getDate(); //取得網站日期時間
date=new Date(ld); //轉換為標准時間對象
String bjTime = df.format(date);
System.out.println("北京時間:"+bjTime);

2. Java 把一個URL中的數據保存成本地文件,文件名為本機當前的系統時間

publicstaticStringdownloadLog(StringloadUrl,StringfileName)throwsException{
URLurl=newURL(loadUrl);

HttpURLConnectionconn=(HttpURLConnection)url.openConnection();

conn.setConnectTimeout(50*1000);

conn.setRequestProperty("User-Agent",
"Mozilla/4.0(compatible;MSIE5.0;WindowsNT;DigExt)");
InputStreaminputStream=null;
FileOutputStreamfos=null;
inputStream=conn.getInputStream();

//路徑目錄
FilesaveDir=newFile("D://test");
if(!saveDir.exists()){
saveDir.mkdirs();
}

Filefile=newFile(saveDir+File.separator+fileName);

fos=newFileOutputStream(file);
readInputStream(fos,inputStream);

returnfile.toString();
}


/**
*用流把數據寫到本地文件上
*
*@paraminputStream
*@return
*@throwsException
*@throwsIOException
*/
(FileOutputStreamfos,
InputStreaminputStream)throwsException{
byte[]buffer=newbyte[1024];
intlen=0;

try{
while((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);

}
fos.flush();

}catch(Exceptione){

logger.error("readInputStream文件可能太大導致");
thrownewException(e);

}finally{

try{

fos.close();
inputStream.close();
}catch(IOExceptione){

}
}

}

閱讀全文

與javaurl時間相關的資料

熱點內容
android訪問html 瀏覽:79
做一個app或者網頁用什麼編程 瀏覽:767
一體機伺服器運行失敗怎麼辦 瀏覽:928
longjava比較大小 瀏覽:522
clojure編程pdf 瀏覽:12
一年級數學圈一圈的演算法 瀏覽:101
完整的python 瀏覽:32
c51單片機數字鍾 瀏覽:915
apk文件反編譯後安裝包損壞 瀏覽:808
app圖標分別是什麼 瀏覽:359
linux全選命令 瀏覽:365
怎麼用華碩app 瀏覽:955
小明台灣通道加密 瀏覽:367
apriori演算法的java 瀏覽:707
jscanvas壓縮圖片 瀏覽:383
單片機與汽車單片機有何不同 瀏覽:964
獸醫雲平台伺服器 瀏覽:316
php循環字母 瀏覽:658
盛達是什麼APP 瀏覽:218
android計算屏幕高度 瀏覽:672