yani su sekilde "201709071826"
gordugumuz gibi bu aslinda hem date hem time iceren bir string. ama bu halde hic bir sey anlasilmiyor. aslinda su sekilde olsa 2017/09/07 18:26 cok daha anlasilir olacak. ama tabiki de bu islemi string olan deger uzerinde yapamayiz. o yuzden once elimizdeki string i date e cevirmemiz lazim. ve simple date format ile var olan formatini parse etmeliyiz yani /siz halini.
Daha sonra elde ettigimiz Date objesi icin / ekleme yapmak amaciyla yeni bir simple date format olusturmaliyiz. Date i ona parse etmeliyiz.
String dateTxt = "201709071826";
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm") ;
try{
date = sdf.parse(dateTxt);
// date eklendi. simdi okunablirligi attirmak amaciyla / eklicez, bunun icin yeni bir simpledateformat olusturcaz
} catch(ParseException e){
e.printStackTrace();
}
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
String formattedDate = sdf2.format(date);
System.out.println(formattedDate); // 2017/09/07 18:26 elde ederiz.
Hiç yorum yok:
Yorum Gönder