Java: How parse String to Date correct? -
i try parse date string:
string datestring = "fr, 1. jan"; dateformat format = new simpledateformat("ee, d. mmm"); date date = null; try { date = format.parse(datestring); } catch (exception e) { e.printstacktrace(); } system.out.println(format.format(date));
and got output these:
do, 1. jan
why did happen , why isn't same output input?
you forgot year. when parse it, in year 1970 (the friday ignored). when parse back, parse date 01 jan 1970, thursday. should work:
string datestring = "fr, 1. jan"; dateformat format = new simpledateformat("ee, d. mmm"); date date = null; try { date = format.parse(datestring); } catch (exception e) { e.printstacktrace(); } date.setyear(new date().getyear()); //alternativ: date.setyear(2016); system.out.println(date); system.out.println(format.format(date));
Comments
Post a Comment