2013年9月8日星期日

mongodb java access date issue

When I use the new Date () to a date type mongodb when deposited , and then I query using dbObject.get ("time"); But so obtained is 1,378,356,771,820 , a long integer , and how to convert Date Type ah ?
------ Solution ---------------------------------------- ----
should use this. .
	public static void main(String[] args) throws ParseException {
long time = 1378356771820L;
Date date = new Date(time);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String newDate = df.format(date);
System.out.println(newDate);
}

------ For reference only ----------------------------------- ----
	public static void main(String[] args) throws ParseException {
String str = "1378356771820";
DateFormat df = new SimpleDateFormat("S");
Date date = df.parse(str);
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String newDate = df.format(date);
System.out.println(newDate);
}

------ For reference only ---------------------------- -----------
Ah , well careless ah, it was my mistake, I use json serialization is not dealt with date type, so it becomes a long integer . . .

then because dbObject.get ("time"). toString (); such data fetched is this : Thu Sep 05 12:52:51 CST 2013, so do the following conversion :
DateFormat df = new SimpleDateFormat ("EEE MMM dd HH: mm: ss zzz yyyy", java.util.Locale.ENGLISH);
Then you can put it into the model 's attribute set up :
model.setTime (df.parse (dbObject.get ("time"). toString ()));

Haha , thank you, to the point ! ! !

没有评论:

发表评论