-1

I've a timestamp which print date and time(24 hours format)

String timeStamp = new SimpleDateFormat("ddMMMyyyyHHmm").format(Calendar.getInstance().getTime());

When I print timeStamp the output is

27Feb20180051

but I want something like this

27Feb20180051EST

Any idea how to proceed?

Zeus07
  • 168
  • 1
  • 5
  • 17
  • 4
    This might be a good time to learn about the Java 8 date/time API. It makes your problem a lot easier. – Tim Biegeleisen Feb 27 '18 at 06:02
  • 1
    Try the format `ddMMMyyyyHHmmz` – Thiyagu Feb 27 '18 at 06:05
  • https://beginnersbook.com/2013/05/java-date-timezone/ – Gokul Nath KP Feb 27 '18 at 06:08
  • Possible duplicate of [SimpleDateFormat with TimeZone](https://stackoverflow.com/questions/37747467/simpledateformat-with-timezone) – Ole V.V. Feb 27 '18 at 07:17
  • IMHO you should avoid the notoriously troublesome `SimpleDateFormat` and its long outdated friends like `Calendar`. [java.time, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) is so much nicer to work with. – Ole V.V. Feb 27 '18 at 07:19
  • What did your search bring up? In what way was it insufficient? There are many questions dealing with formatting of date-times with time zone already. Also what is the reason for requiring that particular format? You may want to consider [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) instead. – Ole V.V. Feb 27 '18 at 07:21
  • @F0XS This question is very likely a duplicate. It’s about converting a date-time into a string with a time zone abbreviation in Java. It’s not a duplicate of that question about converting a string with an offset to Unix time in PHP. – Ole V.V. Feb 27 '18 at 09:00

3 Answers3

1

Try Following code,

SimpleDateFormat dateFormatter = new SimpleDateFormat("ddMMMyyyyHHmmz");
String timeStamp = dateFormatter.format(Calendar.getInstance().getTime());

You can format your date according to your requirement using patterns. More information is included in following link. https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

pavithraCS
  • 709
  • 6
  • 23
1

try the below code

String timeStamp = new SimpleDateFormat("ddMMMyyyyHHmmz").format(Calendar.getInstance().getTime());
Vel Guru
  • 394
  • 1
  • 3
  • 16
1
    String timeStamp = ZonedDateTime.now(ZoneId.of("America/Panama"))
            .format(DateTimeFormatter.ofPattern("ddMMMuuuuHHmmzzz", Locale.ENGLISH));
    System.out.println(timeStamp);

This just printed

27Feb20180408EST

Messages:

  1. Specify explicit time zone. Please substitute your desired time zone if it didn’t happen to be America/Panama. You may specify ZoneId.systemDefault() to use the JVM’s time zone setting, only please be aware that this may be changed by other parts of your program or other programs running in the same JVM.
  2. Specify explicit locale. I took ‘Feb’ for English and suggested Locale.ENGLISH, your preference may differ. Also the time zone abbreviation may be different in different locales.
  3. Use java.time, the modern Java date and time API. Calendar, Date and SimpleDateFormat are long outdated and the last in particular notoriously troublesome, so I recommend you avoid them. The modern API is so much nicer to work with.
  4. Avoid the three and four letter time zone abbreviations if you can. While EST may(!) not be understood as something else than North American Eastern Standard Time (UTC-5), many, possibly most abbreviations are ambiguous and hence prone to be misinterpreted at the other end. A recommended format is ISO 8601, like 2018-02-27T04:08-05:00. It’s unambiguous, and since it’s standard it will be widely understood. You can generate it easily in Java: OffsetDateTime.now(ZoneId.of("America/Panama")).toString().

Link: Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161