How to get in this date format? [duplicate] - java

This question already has answers here:
How can I change the date format in Java? [duplicate]
(10 answers)
Change date format in a Java string
(22 answers)
Java string to date conversion
(17 answers)
Closed 4 years ago.
I am getting two date fields from JSON as text like this May 22 12:05:41 UTC 2018 and 2018-05-22 12:05:41.512 but I have to change to MM-dd-yyyy HH:mm:ss format.

Just to be clear - date/time objects are format agnostic. They are simply containers for the amount of time which has passed since a fixed point in time (usually the Unix Epoch), so you can't change their format per se.
However, you can, generate a String of a prescribed pattern.
When dealing with date/time in Java you should make use of the date/time APIs introduced in Java 8 (or the ThreeTen back port)
For example...
String date1 = "May 22 12:05:41 UTC 2018";
DateTimeFormatter format1 = DateTimeFormatter.ofPattern("MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
LocalDateTime ldt1 = LocalDateTime.parse(date1, format1);
String date2 = "2018-05-22 12:05:41.512";
DateTimeFormatter format2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);
LocalDateTime ldt2 = LocalDateTime.parse(date2, format2);
DateTimeFormatter format3 = DateTimeFormatter.ofPattern("MM-dd-yyyy HH:mm:ss", Locale.ENGLISH);
System.out.println(ldt1.format(format3));
System.out.println(ldt2.format(format3));
Which outputs...
05-22-2018 12:05:41
05-22-2018 12:05:41
Since one of your inputs has a timezone associated with it, it would be appropriate to take it into consideration
ZonedDateTime zdt = ZonedDateTime.parse(date1, format1);
LocalDateTime ldt1 = zdt.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime();
which outputs (for my current location)
05-22-2018 22:05:41

You can use this code
DateFormat format = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
DateFormat inputFormat1 = new SimpleDateFormat("MMM dd HH:mm:ss z yyyy");
DateFormat inputFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String date1 = "May 22 12:05:41 UTC 2018";
String date2 = "2018-05-22 12:05:41.512";
System.out.println(format.format(inputFormat1.parse(date1)));
System.out.println(format.format(inputFormat2.parse(date2)));

Related

Changing date format in Android getting ParseException [duplicate]

This question already has answers here:
Java - Unparseable date
(3 answers)
how to parse output of new Date().toString()
(4 answers)
Closed 2 years ago.
I'm new in Android and Java development.
I have this day type "Fri Jan 27 00:00:00 GMT+00:00 1995". <-- (input)
I want to get "1995/27/01" <-- (output)
I'm using this code :
String inputPattern = "EEE MMM dd HH:mm:ss z yyyy";
String outputPattern = "yyyy-MM-dd";
SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
Date date = inputFormat.parse("Fri Jan 27 00:00:00 GMT+00:00 1995");
String str = outputFormat.format(date);
but I get a ParseException.
Any idea why?
If you were using java.time (available from Java 8), you could do it like this:
public static void main(String[] args) {
// example input String
String datetime = "Fri Jan 27 00:00:00 GMT+00:00 1995";
// define a pattern that parses the format of the input String
String inputPattern = "EEE MMM dd HH:mm:ss O uuuu";
// define a formatter that uses this pattern for parsing the input
DateTimeFormatter parser = DateTimeFormatter.ofPattern(inputPattern, Locale.ENGLISH);
// use the formatter in order to parse a zone-aware object
ZonedDateTime zdt = ZonedDateTime.parse(datetime, parser);
// then output it in the built-in ISO format once,
System.out.println("date, time and zone (ISO):\t"
+ zdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
// then extract the date part
LocalDate dateOnly = zdt.toLocalDate();
// and output that date part using a formatter with a date-only pattern
System.out.println("date only as desired:\t\t"
+ dateOnly.format(DateTimeFormatter.ofPattern("uuuu/dd/MM")));
}
The output of this piece of code is
date, time and zone (ISO): 1995-01-27T00:00:00Z
date only as desired: 1995/27/01
Maybe add applyPattern function in code
String inputPattern = "EEE MMM dd HH:mm:ss z yyyy";
String outputPattern = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(inputPattern);
Date date = sdf.parse("Fri Jan 27 00:00:00 GMT+00:00 1995");
sdf.applyPattern(outputPattern);
String str = sdf.format(date);

Format a date to a specific format in java? [duplicate]

This question already has answers here:
Setting date into "dd"-"mm"-"yy" format from "dd"-"mm"-"yyyy" in java
(4 answers)
Closed 6 years ago.
I have a date in the format 11-NOV-13
When i convert the date i get the value Sat Nov 11 00:00:00 IST 13.
But i want the converted date to be in the format Sat Nov 11 00:00:00 IST 2013.
Though i have used the dateformat it is not taking it. Can anyone point me where i am wrong?
Below is the code i have been working on
DateFormat format = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
Date myDate = format.parse("11-NOV-13");
Change the format to:
DateFormat format = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
You need to make the year 2 numbers:
DateFormat format = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
You can reference the docs here.
As you are not providing the full year you need
DateFormat format = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
Date myDate = format.parse("11-NOV-13");
System.out.println(myDate);
Change to this format:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
DateFormat format = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
Date myDate = format.parse("11-NOV-13");
String formattedDate = sdf.format(myDate);

How to format date? [duplicate]

This question already has answers here:
Convert string to date then format the date
(8 answers)
Closed 9 years ago.
Given DateTime, how to format date in the following format ?
12 JUL 2013
in c#, you do this
string formatedDateString = String.Format("{0:dd MMM yyyy}", myDateTime);
What is the equivalent code in JAVA ?
And date represents instance of java.util.Date
String formattedDate = new SimpleDateFormat("dd MMM yyyy").format(date);
Use a SimpleDateFormat. Along the lines of
(new SimpleDateFormat("dd MMM yyyy")).parse("12 JUL 2013");
(new SimpleDateFormat("dd MMM yyyy")).format(new Date());
I would use the localized approach as described here:
String formattedDate = DateFormat.getDateInstance().format(date);

Java TimeZone conversion issue [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java Timezone: why Offset is needed
My system timezone is Asia/Calcutta.My requirment is to convert time in one timezone to other.
long l = 1223123123232l;// long value representing the date.
TimeZone tz = TimeZone.getTimeZone("Australia/Sydney");// First Time zone
long tzOff = tz.getOffset(l);
java.util.Date d = new Date(l-tzOff); // WHY THIS??
DateFormat df = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
df.setTimeZone(TimeZone.getTimeZone("Africa/Asmara"));// Required Time zone
String s = df.format(d);
System.out.println(s);
What is wrong with my code?
This is a duplicate question and already resolved.
Use Java Timezone: why Offset is needed
OR
Try below
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
System.out.println("Default time, timezone EST : "+dateFormat.format(date));
TimeZone t1 = TimeZone.getTimeZone("Asia/Calcutta");
dateFormat.setTimeZone(t1);
System.out.println("Converted time, timezone IST : "+dateFormat.format(date));

Date format in java [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how to convert seconds_since_the_beginning_of_this_epoch to date format in java..?
hi i have "1304054138" in ssboetod . I want to display it in this "21 Apr 2011 11:46:00 AM IST" format. How i can do this in java code... ?
SimpleDateFormat sdf = new SimpleDateFormat("dd MM yyyy hh:mm:ss a z")
String result = sdf.format(new Date(timestamp));
if timestamp is a String, you can obtain the long version by calling Long.parseLong(string)
Like this:
// creat date format
DateFormat dateFormat = DateFormat.getDateTimeInstance(
DateFormat.LONG, DateFormat.LONG);
// set time zone
dateFormat.setTimeZone(TimeZone.getTimeZone("IST"));
// create and format date
String formattedDate = dateFormat.format(new Date(Long.valueOf("1304054138")));
// write out
System.out.println(formattedDate);

Categories