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