SNMP EventTime in Human Readable format in Java

后端 未结 3 1218
走了就别回头了
走了就别回头了 2021-01-22 06:56

I have a stand alone java application that receives SNMP messages via an SNMP trap. I am using SNMP4J library in my application. In the SNMP message received, I need to convert

3条回答
  •  温柔的废话
    2021-01-22 07:24

    Maybe it's a bit late (you posted your question 6 years ago) but I recently got the same problem and found a general solution that takes into account:
    1. The SNMP-reply may or may not report the offset from GMT
    2. If the time zone is reported, it may be different from our local time zone

    /**********************************************************************************************************************
     * Import definitions
     *********************************************************************************************************************/
    import java.text.SimpleDateFormat;
    import java.util.*;
    
    /**********************************************************************************************************************
     * Class converting an SNMP DateAndTime into something readable.
     *********************************************************************************************************************/
    public class ConvertDateAndTime
    {
      /********************************************************************************************************************
       * This method converts the specified octet string into an array of bytes.
       * 
    The string should be a number of 2-char hexadecimal bytes values separated by any non-hexadecimal character. * * @param value_ipar The value returned by the equipment. * @return The value as an array of bytes. * @throws Exception Thrown in case of an error *******************************************************************************************************************/ public static int[] octetStringToBytes(String value_ipar) { // --------------------------- // Split string into its parts // --------------------------- String[] bytes; bytes = value_ipar.split("[^0-9A-Fa-f]"); // ----------------- // Initialize result // ----------------- int[] result; result = new int[bytes.length]; // ------------- // Convert bytes // ------------- int counter; for (counter = 0; counter < bytes.length; counter++) result[counter] = Integer.parseInt(bytes[counter], 16); // ---- // Done // ---- return (result); } // octetStringToBytes /******************************************************************************************************************** * This method converts the 'DateAndTime' value as returned by the device into internal format. *
    It returns null in case the reported year equals 0. *
    It throws an exception in case of an error. *******************************************************************************************************************/ public static Date octetStringToDate(String value_ipar) throws Exception { // --------------------------- // Convert into array of bytes // --------------------------- int[] bytes; bytes = octetStringToBytes(value_ipar); // ----------------------- // Maybe nothing specified // ----------------------- if (bytes[0] == 0) return (null); // ------------------ // Extract parameters // ------------------ int year; int month; int day; int hour; int minute; int second; int deci_sec = 0; int offset = 0; year = (bytes[0] * 256) + bytes[1]; month = bytes[2]; day = bytes[3]; hour = bytes[4]; minute = bytes[5]; second = bytes[6]; if (bytes.length >= 8) deci_sec = bytes[7]; if (bytes.length >= 10) { offset = bytes[9] * 60; if (bytes.length >= 11) offset += bytes[10]; if (bytes[8] == '-') offset = -offset; offset *= 60 * 1000; } // ------------------------------------ // Get current DST and time zone offset // ------------------------------------ Calendar calendar; int my_dst; int my_zone; calendar = Calendar.getInstance(); my_dst = calendar.get(Calendar.DST_OFFSET); my_zone = calendar.get(Calendar.ZONE_OFFSET); // ---------------------------------- // Compose result // Month to be converted into 0-based // ---------------------------------- calendar.clear(); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month - 1); calendar.set(Calendar.DAY_OF_MONTH, day); calendar.set(Calendar.HOUR_OF_DAY, hour); calendar.set(Calendar.MINUTE, minute); calendar.set(Calendar.SECOND, second); calendar.set(Calendar.MILLISECOND, deci_sec * 100); // --------- // Reset DST // --------- calendar.add(Calendar.MILLISECOND, my_dst); // ----------------------------------------------------------------------------------- // If the offset is set, we have to convert the time using the offset of our time zone // ----------------------------------------------------------------------------------- if (offset != 0) { int delta; delta = my_zone - offset; calendar.add(Calendar.MILLISECOND, delta); } // ------------- // Return result // ------------- return (calendar.getTime()); } // octetStringToDate /******************************************************************************************************************** * M A I N *******************************************************************************************************************/ public static void main(String[] args) { try { SimpleDateFormat format; format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz"); Date result; result = octetStringToDate("07 E2 02 02 12 0C 27 00"); // 18:12 in local time zone System.out.println(format.format(result)); // "2018-02-02 18:12:39 CET" result = octetStringToDate("07 E2 02 02 12 0C 27 00 2B 08 00"); // 18:12+08:00 System.out.println(format.format(result)); // "2018-02-02 11:12:39 CET" result = octetStringToDate("07 E2 02 02 12 0C 27 00 2D 04 00"); // 18:12-04:00 System.out.println(format.format(result)); // "2018-02-02 23:12:39 CET" } catch (Exception exception_ipar) { exception_ipar.printStackTrace(); } } // main } // class ConvertDateAndTime

提交回复
热议问题