java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy

Date is a container for the number of milliseconds since the Unix epoch ( 00:00:00 UTC on 1 January 1970). It has no concept of format. Java 8+ LocalDateTime ldt = LocalDateTime.now(); System.out.println(DateTimeFormatter.ofPattern(“MM-dd-yyyy”, Locale.ENGLISH).format(ldt)); System.out.println(DateTimeFormatter.ofPattern(“yyyy-MM-dd”, Locale.ENGLISH).format(ldt)); System.out.println(ldt); Outputs… 05-11-2018 2018-05-11 2018-05-11T17:24:42.980 Java 7- You should be making use of the ThreeTen Backport Original Answer For … Read more

Java SimpleDateFormat(“yyyy-MM-dd’T’HH:mm:ss’Z'”) gives timezone as IST

You haven’t set the timezone only added a Z to the end of the date/time, so it will look like a GMT date/time but this doesn’t change the value. Set the timezone to GMT and it will be correct. SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd’T’HH:mm:ss’Z'”); sdf.setTimeZone(TimeZone.getTimeZone(“GMT”));

Javascript add leading zeroes to date

Try this: http://jsfiddle.net/xA5B7/ var MyDate = new Date(); var MyDateString; MyDate.setDate(MyDate.getDate() + 20); MyDateString = (‘0’ + MyDate.getDate()).slice(-2) + “https://stackoverflow.com/” + (‘0’ + (MyDate.getMonth()+1)).slice(-2) + “https://stackoverflow.com/” + MyDate.getFullYear(); EDIT: To explain, .slice(-2) gives us the last two characters of the string. So no matter what, we can add “0” to the day or month, and … Read more

Change date format in a Java string

Use LocalDateTime#parse() (or ZonedDateTime#parse() if the string happens to contain a time zone part) to parse a String in a certain pattern into a LocalDateTime. String oldstring = “2011-01-18 00:00:00.0”; LocalDateTime datetime = LocalDateTime.parse(oldstring, DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss.S”)); Use LocalDateTime#format() (or ZonedDateTime#format()) to format a LocalDateTime into a String in a certain pattern. String newstring = datetime.format(DateTimeFormatter.ofPattern(“yyyy-MM-dd”)); … Read more