The java.text.SimpleDateFormat
class is used to both parse and format dates according to a formatting pattern you specify yourself
Approach :1 One Time Zone to Another Time Zone date converter
import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.TimeZone;public class DateFormat {public static void main(String[] args) {Calendar calendar = Calendar.getInstance();calendar.setTime(new Date());SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");sdf.setTimeZone(TimeZone.getTimeZone("UTC"));System.out.println("UTC: " + sdf.format(calendar.getTime()));sdf.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));System.out.println("Europe: " + sdf.format(calendar.getTime()));sdf.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));System.out.println("Indian: " + sdf.format(calendar.getTime()));}}
SimpleDate Formatter Syntax
y Year (e.g. 12 or 2012). Use either yy or yyyy.M Month in year. Number of M's determine length of format (e.g. MM, MMM or MMMMM)d Day in month. Number of d's determine length of format (e.g. d or dd)h Hour of day, 1-12 (AM / PM) (normally hh)H Hour of day, 0-23 (normally HH)m Minute in hour, 0-59 (normally mm)s Second in minute, 0-59 (normally ss)S Millisecond in second, 0-999 (normally SSS)E Day in week (e.g Monday, Tuesday etc.)D Day in year (1-366)F Day of week in month (e.g. 1st Thursday of December)w Week in year (1-53)W Week in month (0-5)a AM / PM markerk Hour in day (1-24, unlike HH's 0-23)K Hour in day, AM / PM (0-11)z Time Zone
Example :
Date Patter Exampledd-MM-yy 30-03-21dd-MM-yyyy 30-02-2021MM-dd-yyyy 03-30-2021yyyy-MM-dd 2021-03-30yyyy-MM-dd HH:mm:ss 2021-03-30 23:59:59yyyy-MM-dd HH:mm:ss.SSS 2021-03-30 23:59:59.999yyyy-MM-dd HH:mm:ss.SSSZ 2021-03-30 23:59:59.999+0100EEEEE MMMMM yyyy HH:mm:ss.SSSZ Tuesday March 2021 10:45:42.720+0100
No comments:
Post a Comment