How to convert timestamp in mongo to normal date format

Scenario: Due to the conversion of the date format in the back end during the business development process, a timestamp similar to NumberLong (1476407933621) is generated in mongo according to the system time, and we need to convert it into a well-known date format to facilitate viewing the date. .

This record is convenient for dealing with similar problems later. The code is only for use, and I hope to discuss and correct the imprecise places.

The Json plugin uses Gson, and the package is imported before doing it

date after conversion

======================BEGIN======================
The first visit time is: 2016-10-14 09:18:22
The 2nd visit time is: 2016-10-14 09:18:53
The 3rd visit time is: 2016-10-22 14:18:49
The 4th visit time is: : 2016-10-22 14:20:18
5th visit time: 2016-10-22 14:32:45
6th visit time: 2016-10-22 14:33:31
7th visit time For:
2016-10-22 14:40:32 8th visit time: 2016-10-23 00:05:18
9th visit time: 2016-10-23 16:42:06
10th visit The time is: 2016-10-24 11:52:31
======================END================ ========

Step 1: Take the data out of mongo and create a text file to save it

 Data format in mongo (refer to your actual business for specific data)

/* 1 */
{
}

/* 2 */
{
}

Step 2: Execute the following code

 

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.google.gson.Gson;
import com.google.gson.JsonArray;

/**
 * Convert timestamp in mongo to normal date format
 *
 * @author Mr.Cui
 *
 */
public class testTime {

	public static void main(String[] args) {
		System.out.println("=====================BEGIN=====================");
		String filePath = "C:/Users/Administrator/Desktop/stamepToDate.txt";
		beginChange(filePath);
		System.out.println("=====================END=======================");
	}

	/**
	 * start conversion
	 */
	public static void beginChange(String filePath) {
		File file = new File(filePath);
		if (file.exists() && file.length() == 0) {
			System.out.println("The file content is empty");
			return;
		}
		StringBuilder result = new StringBuilder();
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));// Construct a BufferedReader class to read the file
			String s = null;
			while ((s = br.readLine()) != null) {// Use the readLine method to read one line at a time
				if (!s.contains("*")) {
					s = (System.lineSeparator() + s);
					if (s.contains("}")) {
						s = s + ",";
					}
					result.append(s);
				}
			}
			br.close();
			changeAndOutDate(result);
		} catch (Exception e) {
			e.printStackTrace ();
			System.out.println("The data was not successfully converted when an abnormality occurred in the system");
			return;
		}
	}

	/**
	 * Change string timestamp to date format similar to 2016-10-24 11:52:31
	 *
	 * @param result
	 * read string
	 */
	public static void changeAndOutDate(StringBuilder result) {

		Gson gson = new Gson ();
		String jsonArrayStr = "[" + result.substring(0, result.length() - 1) + "]";
		JsonArray jsonArray = gson.fromJson(jsonArrayStr.toString(), JsonArray.class);
		for (int i = 0; i < jsonArray.size(); i++) {
			String str = jsonArray.get(i).getAsJsonObject().get("create_time").getAsString();
			str = str.substring(str.indexOf("(") + 1, str.lastIndexOf(")"));
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			long lt = new Long(str);
			Date date = new Date(lt);
			str = simpleDateFormat.format(date);
			System.out.println("The first" + (i + 1) + "The access time is:" + str);
		}

	}
}

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326933771&siteId=291194637