2012-03-14 96 views
1

我有一個獨立的java應用程序,它通過SNMP陷阱接收SNMP消息。我在我的應用程序中使用SNMP4J庫。在收到的SNMP消息中,我需要將事件時間字段(十六進制格式)轉換爲人類可讀的格式。本次活動時間字段正常接收查找舉例如下:Java中人類可讀格式的SNMP EventTime

 eventTime*SNMPv2-SMI::enterprises.193.183.4.1.4.5.1.7.0 = Hex-STRING: 
07 DC 03 0C 12 15 2C 1F 2B 01 00 

誰能告訴我怎麼可以將文本轉換'07 DC 03 0C 12 15 2C 1F 2B 01 00' 與或人類可讀的日期時間值沒有SNMP4J庫的幫助?謝謝。

+0

更確切地說,我需要得到一個java.util.Date對象了這個十六進制數字序列。我想知道是否有辦法做到這一點。 – 2012-03-14 10:28:27

回答

1

你可以使用Integer.parseInt(「07dc」,16)和2012年的流行音樂,所以這應該提供一年的暗示,其餘的我相信你會自己弄清楚如果這確實是一年。

+0

String [] tmp =(pdu.getVariableBindings()。get(8))。toValueString()。split(「:」); String eventTime = getTimeStampAsText(tmp); – 2012-03-14 11:47:20

+0

private synchronized String getTimeStampAsText(String [] strArray){ String hyear,hmonth,hday,hhour,hmin,hmil; int染色,dmonth,dday,dhour,dmin,dmil; hyear = strArray [0] + strArray [1]; 012monhmonth = strArray [2]; hday = strArray [3]; – 2012-03-14 11:48:08

+0

hhour = strArray [4]; hmin = strArray [5]; hmil = strArray [6]; dyear = Integer.parseInt(hyear,16); dmonth = Integer.parseInt(hmonth,16); dday = Integer.parseInt(hday,16); dhour = Integer.parseInt(hhour,16); dmin = Integer.parseInt(hmin,16); dmil = Integer.parseInt(hmil,16); return dyear +「 - 」+ dmonth +「 - 」+ dday +「」+ dhour +':'+ dmin +':'+ dmil; } – 2012-03-14 11:48:40

0

也許這是一個有點晚了(你6年前發佈你的問題),但我最近得到了同樣的問題,結果發現,考慮到一個通用的解決方案:
1. SNMP的答覆可能會或可能不會報告偏移從GMT
2.如果時區報告,也可能是從我們的本地時區不同

/********************************************************************************************************************** 
* 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. 
    * <br>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. 
    * <br>It returns <code>null</code> in case the reported year equals 0. 
    * <br>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