2013-07-30 4 views
1

은 내가 유닉스 타임 스탬프로 날짜 문자열을 변환하려면이 코드를 사용 :이이 날짜/시간을 represant한다java : 캘린더에 올바른 날짜/시간이 표시되지 않습니까?

int year = 2012; 
int month = 2; // eg. for march 
int day = 31; 
int hrs = 0; 
int min = 18; 

영어 표기

에서 2012년 3월 31일 0시 18분 또는 2012년 3월 31일 0시 18분 내가 ( cal.setTimeInMillis(1333145929 * 1000);을)를 다시 변환하면 내가 30.3.2012 00:18

있어 1333145929 언젠가 손실 :

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); // GMT+1 
cal.set(year, month, day, hrs, min); 
unixtime = cal.getTimeInMillis()/1000; 

unixtime의 결과입니다! 인쇄 위의 나를 위해

+0

??? 예를 들어 지구 반대편? – Deckard27

+1

문제를 보여주는 완벽한 예제를 게시 할 수 있습니까? – assylias

답변

2

를 시도?

나는 GMT 시간을 얻고 있다고 생각하는데, 이것은 한 시간 뒤에 있기 때문에 자정에 1 시간 늦는다.

고려 :

public static void main(String[] args) throws Exception { 
    int year = 2012; 
    int month = 2; // eg. for march 
    int day = 31; 
    int hrs = 0; 
    int min = 18; 
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); // GMT+1 
    cal.set(year, month, day, hrs, min); 
    long time = cal.getTimeInMillis(); 
    System.out.println(time); 
    cal.setTimeInMillis(time); 
    final DateFormat sdf = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.FULL, SimpleDateFormat.FULL); 
    System.out.println(sdf.format(cal.getTime())); 
} 

출력 :

final DateFormat sdf = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.FULL, SimpleDateFormat.FULL); 
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Berlin")); 
System.out.println(sdf.format(cal.getTime())); 

내가 얻을 : 지금은 DateFormatTimeZone에 추가하면

1333145915825 
Friday, 30 March 2012 23:18:35 o'clock BST 

,435,428,282,573 이 포맷하고 하지 CalendarTimeZone (당신이 부르는이 Calendar.getTime()) 때 (210)

는 그래서 SimpleDateFormat은 기본 TimeZone을 사용합니다.

+0

감사합니다.하지만 첫 번째 예제에서 나는 1333145915825 을 얻었고 두 번째 것은 1333145887761입니다. sdf.setTimeZone은 sdf.format에만 적용되며 시간을 직접 출력합니까? – Mike

+0

첫 번째 예제와 두 번째 예제 사이에 시간이 있었기 때문에 ... –

0
int year = 2012; 
    int month = 2; // eg. for march 
    int day = 31; 
    int hrs = 0; 
    int min = 18; 


    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); // GMT+1 
    cal.set(year, month, day, hrs, min); 
    long unixtime = cal.getTimeInMillis(); 

    cal.setTimeInMillis(unixtime); 

    //Lets print it! 
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 
    sdf.setTimeZone(TimeZone.getTimeZone("Europe/Berlin")); 
    System.out.println(sdf.format(cal.getTime())); 

코드 : 당신이 당신의 날짜를 인쇄하려면 어떻게 03/31/2012 00:18:56

?

+0

저도 마찬가지지만, 이것 좀보세요 : http : // ideone.com/oCBFRF – jlordo

+1

@jlordo SimpleDateFormat의 시간대를 설정하지 않았습니다. – nkukhar

+0

@jlordo 여기를보십시오. http://ideone.com/4zWQ1x – nkukhar

0

나는이 코드를 시도했다 :

int year = 2012; 
    int month = 2; // eg. for march 
    int day = 31; 
    int hrs = 0; 
    int min = 18; 

    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); // GMT+1 
    cal.set(year, month, day, hrs, min); 
    long unixtime = cal.getTimeInMillis()/1000; 

    System.out.println(cal.getTime()); 

    System.out.println(unixtime); 

    cal.setTimeInMillis(unixtime * 1000); 

    System.out.println(cal.getTime()); 

그리고 출력은 이것이다 :

Sat Mar 31 00:18:04 CEST 2012 
1333145884 
Sat Mar 31 00:18:04 CEST 2012 
0

문제는 당신이 2012년 3월 30일 0시 18분을 얻는 방법이다. 달력 날짜 필드가 OK이다, 당신은 어떻게 Calendar를 인쇄이

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin")); // GMT+1 
    cal.set(year, month, day, hrs, min); 
    long unixtime = cal.getTimeInMillis()/1000; 
    cal.setTimeInMillis(unixtime * 1000); 
    System.out.println(cal.get(Calendar.DATE)); 

출력

31 
0
int year = 2012; 
     int month = 2; // eg. for march 
     int day = 31; 
     int hrs = 0; 
     int min = 18; 

     Calendar cal = Calendar.getInstance(TimeZone 
       .getTimeZone("Europe/Berlin")); 
     cal.set(year, month, day, hrs, min); 
     long unixtime = cal.getTimeInMillis()/1000; 
     cal.setTimeInMillis(unixtime * 1000); 
     System.out.println(cal.getTime()); 

출력 :

토 월 다른 getTimeZone의로 사용해 볼 수 (31) 인도 표준시 03시 48분 10초 2012

관련 문제