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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -