2014-09-11 7 views
6

के युग के बाद से मैं इसConvert समय "dd/mm/yy"

import org.joda.time.DateTime; 
import org.joda.time.DateTimeZone; 
import org.joda.time.format.DateTimeFormat; 
import org.joda.time.format.DateTimeFormatter; 


private static final DateTimeZone PST = DateTimeZone.forID("PST8PDT"); 
private static final DateTimeFormatter DATE_FORMATTER = 
    DateTimeFormat.forPattern("yyyy-MM-dd").withZone(PST); 


Long millis = DateTime.parse(startDate, DATE_FORMATTER).withTimeAtStartOfDay().getMillis()); 

की तरह मिली सेकंड के लिए एक तिथि स्ट्रिंग परिवर्तित कर रहा हूँ जहाँ STARTDATE तारीख मैं परिवर्तित करना चाहते है।

जब मैं मिलिस प्राप्त करता हूं तो पीएसटी में तारीख प्राप्त करने के लिए मैं इसे कैसे उलटा कर सकता हूं?

+3

आपको "पीएसटी" या "पीएसटी 8 पीडीटी" के बजाय "अमेरिका/लॉस_एंजेलस" का उपयोग करना चाहिए क्योंकि जोडा-टाइम दृढ़ता से पहले प्रारूप को पसंद करता है। टाइमज़ोन नाम और उनके संक्षेप अक्सर अद्वितीय नहीं होते हैं। पीएसटी का मतलब पाकिस्तान मानक समय भी हो सकता है, और जोडा-टाइम में ऐसे संक्षेपों को पार करने में बड़ी समस्याएं हैं। –

उत्तर

2

अगर मैं तुम्हें DateTimeFormatter.print(long) इस्तेमाल कर सकते हैं अपने प्रश्न को समझते हैं,

DateTimeFormatter shortFormat = 
    DateTimeFormat.forPattern("MM/dd/yy").withZone(PST); 
String formatted = shortFormat.print(millis); 
जुड़ा हुआ जावाडोक से

,

एक स्ट्रिंग के लिए एक millisecond तत्काल प्रिंट करता है।

0

बस एक नया DateTime ऑब्जेक्ट का निर्माण करना चाहिए।

DateTime newDate = new DateTime(millis); 

Overloaded with timezone

+0

मैं इसके साथ टाइमज़ोन कैसे निर्दिष्ट करूं? – Shan

+0

इसे दूसरे पैरामीटर, टाइमज़ोन के साथ अधिभारित किया जा सकता है। मुझे यकीन नहीं है कि पीएसटी के लिए सही मूल्य क्या है, लेकिन मुझे यकीन है कि आप इसे पा सकते हैं। पोस्ट में लिंक करें। – Grice

1

एक जावा जो समय क्षेत्र पर विचार millisecond के लिए एक दिया स्ट्रिंग और स्ट्रिंग तारीख को मिलीसेकंड में परिवर्तित कर देंगे java.time एपीआई का उपयोग कर 8 समाधान:

import java.time.Instant; 
import java.time.LocalDateTime; 
import java.time.ZoneId; 
import java.time.ZonedDateTime; 
import java.time.format.DateTimeFormatter; 


public class SO25788709 { 

    public static void main(String[] args) { 
     String strDate = "2014-09-12 23:59:59"; 
     String pattern = "yyyy-MM-dd HH:mm:ss"; 
     DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); 
     ZoneId zone = ZoneId.of("America/Los_Angeles"); 

     long milli = getMillis(strDate, formatter, zone);      
     System.out.println(milli); 

     String retStrDate = getDateString(milli, formatter, zone); 
     System.out.println(retStrDate); 
    } 

    private static long getMillis(String strDate, DateTimeFormatter formatter, ZoneId zone) { 
     LocalDateTime localDateTime = LocalDateTime.parse(strDate, formatter); 
     ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zone); 
     Instant instant = zonedDateTime.toInstant(); 
     long milli = instant.toEpochMilli(); 
     return milli; 
    } 

    private static String getDateString(long milli, DateTimeFormatter formatter, ZoneId zone) { 
     Instant instant = Instant.ofEpochMilli(milli); 
     ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, zone); 
     String strDate = zonedDateTime.format(formatter); 
     return strDate; 
    } 
} 
2

2 तरीकों कि इतनी छूट है बनाया है, किसी भी तारीख को संभालने के लिए किसी भी समय क्षेत्र पर प्रारूप।

सबसे पहले विधि (युग) Millis के लिए तारीख स्ट्रिंग से है

//dateString to long 
    private static long formatterDateToMillis(String dateString, String format, String timeZone){ 
     //define Timezone, in your case you hardcoded "PST8PDT" for PST 
     DateTimeZone yourTimeZone = DateTimeZone.forID(timeZone); 
     //define your pattern 
     DateTimeFormatter customFormat = DateTimeFormat.forPattern(format).withZone(yourTimeZone); 
     //parse dateString to the format you wanted 
     DateTime dateTime = customFormat.parseDateTime(dateString); 
     //return in Millis, usually in epoch 
     return dateTime.getMillis();  
    } 

दूसरा विधि Millis से दिनांक स्ट्रिंग है

//dateInMillis to date format yyyy-MM-dd 
    private static String formatterMillistoDate(long dateInMillis, String format, String timeZone){ 
     //define your format 
     DateTimeFormatter customFormat = DateTimeFormat.forPattern(format); 
     //convert to DateTime with your desired TimeZone 
     DateTime dateTime = new DateTime(dateInMillis, DateTimeZone.forID(timeZone)); 
     //return date String in format you defined 
     return customFormat.print(dateTime); 
    } 

अपने main() प्रणाली के लिए इन इनपुट का प्रयास करें:

long valueInMillis = formatterDateToMillis("2015-03-17","yyyy-MM-dd","PST8PDT"); 
    System.out.println(valueInMillis); 

    String formattedInDate = formatterMillistoDate(1426575600000L,"yyyy-MM-dd","PST8PDT"); 
    System.out.println(formattedInDate); 

आपको निम्न आउटपुट प्राप्त करना चाहिए:

2015-03-17

आशा इस मदद करता है! ;)