A. java時間格式轉換問題
String str = "2013-10-10";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = df.parse(str);
System.out.println(df.format(date));
System.out.println(df.format(new Date()));
一個日期類型的date,只在存在運行過程中,是日期類型。輸出到控制台的過程,已經轉成String類型了。就是說,一調用 println() 內部就已經調用Date的toString方法來轉換成String類型了
B. JAVA中日期格式轉換:2012-07-10 00:00:00.000如何轉換成2012年07月10日
Java時間格式轉換大全
import java.text.*;
import java.util.Calendar;
public class VeDate {
/**
* 獲取現在時間
*
* @return 返回時間類型 yyyy-MM-dd HH:mm:ss
*/
public static Date getNowDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
ParsePosition pos = new ParsePosition(8);
Date currentTime_2 = formatter.parse(dateString, pos);
return currentTime_2;
}
Java是一門面向對象編程語言,不僅吸收了C++語言的各種優點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特徵。Java語言作為靜態面向對象編程語言的代表,極好地實現了面向對象理論,允許程序員以優雅的思維方式進行復雜的編程。
C. Java date 時間戳 怎麼轉換為 C# 時間戳
java的datetime類型用c#實現就是:
String timeStamp = GetTimestamp(DateTime.Now);
時間戳是從1970年0時0分0秒開始到現在的秒數.使用它來獲得的是一個INT值,儲存在資料庫里只要使用INT格式就可以了,方便資料庫進行排序,搜索,而且比datetime格式更節省資料庫空間。
通常用以下方法轉換實現:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace test.Controllers
{
public class TimeStampController : Controller
{
//
// GET: /TimeStamp/
public ActionResult Index()
{
ViewBag.TimeStamp = ConvertDateTimeInt(DateTime.Now);
return View("TimeStamp");
}
public ActionResult GetTimeView(string timeStamp)
{
ViewBag.TimeStamp = GetTime(timeStamp);
return View("TimeStamp");
}
/// <summary>
/// 時間戳轉為C#格式時間
/// </summary>
/// <param name="timeStamp">Unix時間戳格式</param>
/// <returns>C#格式時間</returns>
public static DateTime GetTime(string timeStamp)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(timeStamp + "0000000");
TimeSpan toNow = new TimeSpan(lTime);
return dtStart.Add(toNow);
}
/// <summary>
/// DateTime時間格式轉換為Unix時間戳格式
/// </summary>
/// <param name="time"> DateTime時間格式</param>
/// <returns>Unix時間戳格式</returns>
public static int ConvertDateTimeInt(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
return (int)(time - startTime).TotalSeconds;
}
}
}
D. java中怎麼轉換時間的格式
這個很基本的:
我在前期JAVA WEB開發中就經常用
首先你要弄懂兩個類:一個是java.util.Date;另一個就是java.sql.Date;用強制轉換
給你最簡單的吧也是最好記的。結果集(ResultSet )的getObject("資料庫中的欄位名")取出數據。再進行相應的轉換;如果是存儲就用結果集(ResultSet )的setObject("資料庫中的欄位名")保存數據;
E. java把時間戳轉換成具體的時間的格式
代碼如下:
package date;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static String stampToDateStr(String timeStampStr, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date(Long.valueOf(timeStampStr).longValue()));
}
public static void main(String[] args) {
String date_format = stampToDateStr("1600761641396", "yyyy-MM-dd HH:mm:ss");
System.out.println(date_format);
}
}
輸出:
F. java時間格式轉換
Dated=newDate();
SimpleDateFormats=newSimpleDateFormat("yyyy/MM/ddHH:mma",Locale.ENGLISH);System.out.println(s.format(d));
G. java如何把時間格式轉為毫秒
獲取毫秒數,即long類型的數值,僅能返回自 1970 年 1 月 1 日 00:00:00 GMT 以來的毫秒數。
一樓、二樓的回答就是正確的,不過在使用中還需要根據自身使用環境,直接使用或者進一步按需優化後再使用。
最常使用的就是,把String類型的日期先轉換為Date類型,最後直接調用.getTime()即可,這也是比較方便的了。
還有就是以上提到的Timestamp類中的valueOf(String s) 方法,這里一定要注意,給定的字元串日期型數據必須符合置頂指定格式:yyyy-mm-dd hh:mm:ss[.fffffffff],否則會拋出異常。
PS>
H. java TimeStamp 轉換為 yyyy-MM-dd格式的date類型
實現思路就是先將Timestamp轉換為字元串,之後字元串轉換為日期類型。舉例:Long l = System.currentTimeMillis();//獲取當前的Timestamp值
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//定義日期類型格式
String str2 = Timestamp.valueOf(format.format(l));//轉換為字元串
//System.out.println(str2);//列印獲取的字元串
Date date = format .parse(str2);//格式化獲取到的日期,
System.out.println(date);
輸出結果:2015-06-27。
I. java 時間格式轉換
importjava.text.DateFormat;
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.Date;
publicclassTest{
publicstaticvoidmain(String[]args){
Stringstr="2013-1-12";
DateFormatformat1=newSimpleDateFormat("yyyy-MM-dd");
DateFormatformat2=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
try{
Datedate=format1.parse(str);
str=format2.format(date);
System.out.println(str);
}catch(ParseExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
J. 如何將JAVA DATE類型的日期 轉換成指定格式類型的 (如:YYYY-MM-DD) 的 DATE類型數據
Date類型並沒有格式,只有轉換成String格式的時候讓格式化顯示。
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")format(new Date());
Calendar calendar = Calendar.getInstance();
int year = Integer.parseInt(datetime.substring(0,4));
int month = Integer.parseInt(datetime.substring(5,7));
int date = Integer.parseInt(datetime.substring(8,10));
int hour = Integer.parseInt(datetime.substring(11,13));
int minute = Integer.parseInt(datetime.substring(14,16));
//int second = Integer.parseInt(datetime.substring(17,19));
if(calendar.get(Calendar.YEAR)>year){
int y = calendar.get(Calendar.YEAR)-year;
(10)java時間戳格式轉換擴展閱讀:
Date類可以在java.util包中找到,用一個long類型的值表示一個指定的時刻。它的一個有用的構造函數是Date(),創建一個表示創建時刻的對象。getTime()方法返回Date對象的long值。
import java.util.*;
public class Now {
public static void main(String[] args) {
Date now = new Date();
long nowLong = now.getTime();
System.out.println("Value is " + nowLong);