这是我的代码:
此方法是数据的第一个入口
private void parseAppAnalytics(JSONObject messageBody, String md5, String messageId) throws JSONException {
String key = "Events";
JSONArray appAnalyticsJsonArray = messageBody.getJSONArray(DataSetsConstants.APPANALYTICS);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < appAnalyticsJsonArray.length(); i++) {
JSONObject appAnalyticsJson = appAnalyticsJsonArray.getJSONObject(i);
builder.append(messageId).append(COMMA);
builder.append(md5).append(COMMA);
builder.append(appAnalyticsJson.optString(DataSetsConstants.APPANALYTICS_GAID, "")).append(COMMA);
builder.append(appAnalyticsJson.optString(DataSetsConstants.APPANALYTICS_APP_NAME, "")).append(COMMA);
builder.append(appAnalyticsJson.optString(DataSetsConstants.APPANALYTICS_APP_VERSION, "")).append(COMMA);
builder.append(appAnalyticsJson.optString(DataSetsConstants.APPANALYTICS_EVENT_NAME, "")).append(COMMA);
builder.append(getDateFormatter(appAnalyticsJson.optString(DataSetsConstants.APPANALYTICS_EVENT_TIME, ""))).append(COMMA);
builder.append(messageReceivedTS).append(COMMA);
logger.debug("***In Parser: parse App Analytics: value: " + builder.toString());
writeInFile(key, builder.toString());
builder=new StringBuilder();
}
}
它来到这里:
public final String getDateFormatter(String timeinms) {
try {
Long l = Long.parseLong(timeinms);
Date date = new Date(l);
DateFormat format = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss.SSS");
String date_time = format.format(date);
return date_time;
当我使用日期对象转换为迄今为止的长毫秒(在当地时间)时,它会将时间零件转换为UTC,这在我的情况下是不可预见的。我需要停止此转换。与SimpleDateFormat有什么关系,我是否缺少某些内容?
您可以直接将长时间的米数字传递给格式()方法
public final String getDateFormatter(String timeinms) {
try {
Long l = Long.parseLong(timeinms);
SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss.SSS");
sdf.setTimeZone(TimeZone.getTimeZone("GMT+0.00"));//put your timezone here otherwise use "GMT+0.00" for default GMT timezone
String date_time = format.format(l);
return date_time;
}catch(Exception e){
}
return "";
}