maybe better to understand: my methode get 2 strings: Value and pattern, when the value is 00082017 //ddMMyyyy and the pattern is ddMMyyyy now in my methode i have 3 Strings: day = month = and year = now i will fill the strings day = 00 month = 08 year = 2017 how can i do this? my example from the first post works, but what is when i get value 09002017 and pattern MMddyyyy then is it not working So my code is like. String a= "2011"; SimpleDateFormat formatter = new SimpleDateFormat ("yyMM"); formatter.setTimeZone (TimeZone.getTimeZone ("UTC")); Date date = formatter.parse (a); System.out.println (date); Now problem is, when I am passing 2011 the out it gives is Sat Oct 31 17:00:00 PDT 2020. Here you can see I am passing 11 as month but Formatting dates depends on you and your needs. In some countries, the month comes before the day, then the year (06/22/2022). In others, the day comes before the month, then the year (22/06/2022), and lots more. Regardless of the format, you want to break down the date object value and get the necessary information you want for your web page. For the first day of the current month, you can use a similar trick: Date firstday = new SimpleDateFormat ("yyyyMMdd").parse ( new SimpleDateFormat ("yyyyMM").format (new Date ())+"01"); @jarnbjo I'm sorry, I rechecked the question and your answer and found out that I had totally misunderstood the problem. .

java date year month day