Java – How to get the current date and time zone in numeric format

How to get the current date and time zone in numeric format… here is a solution to the problem.

How to get the current date and time zone in numeric format

I want to print the current date 2017/06/05 > year/month/day in this format
Next to it, the current time zone format is +3

I’ve used this code

String DateToday  = DateFormat.getDateInstance().format(new Date());
String TZtoday = DateFormat.getTimeInstance().getTimeZone().getDisplayName();
txt.setText(DateToday + " | " + TZtoday );

However, it is displayed as follows:

Jun 5, 2017 | Arabia Standard Time

I want this :

2017/06/05 | +3

Solution

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd | X");
System.out.println(sdf.format(new Date()));

Close, but time zone printing has leading zeros:

2017/06/05 | +03

I guess you can remove leading zeros from the timezone if needed:

SimpleDateFormat date = new SimpleDateFormat("yyyy/MM/dd");
SimpleDateFormat zone = new SimpleDateFormat("ZZZZZ");  = +03:00
String tz = zone.format(new Date()).split(":")[0]
    .replaceAll("^(\\+|-)0", "$1");  = +3
System.out.println(sdf.format(new Date()) + " | " + tz);

Give:

2017/06/05 | +3

Related Problems and Solutions