2017-09-11 6 views
-2

Voici mon code:Java objet date auto conversion à l'erreur UTC

cette méthode est la première entrée de données

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(); 
    } 


} 

et il vient ici:

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; 

Quand je convertir long millisecondes (qui est en heure locale) à ce jour en utilisant l'objet date, il convertit la partie temps en UTC, ce n'est pas prévu dans mon cas. J'ai besoin d'arrêter cette conversion. Y at-il quelque chose à faire avec simpledateformat, ai-je raté quelque chose?

+1

timeinMilliseconds au format() Il utilise votre fuseau horaire par défaut. – assylias

+0

mais je passe le temps également à travers la date constructeur – masSdev

+1

Un objet 'java.util.Date' ne contient pas d'informations de fuseau horaire - l'objet est juste un horodatage, et il ne contient aucune information sur un fuseau horaire. Si vous souhaitez l'afficher dans un fuseau horaire particulier, définissez le fuseau horaire sur l'objet 'SimpleDateFormat'. Mais il vaudrait beaucoup mieux utiliser les classes du paquet 'java.time' (Java 8) au lieu des anciennes' 'java.util.Date' et' java.text.SimpleDateFormat'. – Jesper

Répondre

0

Vous pouvez directement passer les longues méthode

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 ""; 
} 
+0

je ne veux pas de conversion de fuseau horaire, je dois analyser le temps dans quel fuseau horaire j'ai reçu – masSdev

+1

@masSdev Comment voulez-vous connaître le fuseau horaire d'une valeur longue? – Tom

+0

Je ne veux pas connaître le fuseau horaire, je dois arrêter cette conversion en UTC pour le temps en millis – masSdev