This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Closed 7 years ago.
I have an XML document that returns the following value for a date-time stamp:
Wed, 18 Feb 2015 22:38:00 +0000
How can I change this (using Java), so I can get this:
Wednesday, February 18, 2015
Have you this date as a string in java?
Try this:
String stringdate = "Wed, 18 Feb 2015 22:38:00 +0000";
//Convert from string to date
DateFormat stringformat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
Date date = stringformat.parse(stringdate);
//Convert from date to string
DateFormat newformat = new SimpleDateFormat("EEEE, MMMM d, yyyy ", Locale.ENGLISH);
System.out.println(newformat.format(date));
With timestamp:
Timestamp d = new Timestamp(System.currentTimeMillis());
//Convert from date to string
DateFormat newformat = new SimpleDateFormat("EEEE, MMMM d, yyyy ", Locale.ENGLISH);
System.out.println(newformat.format(d));
In Java, using SimpleDateFormat, the following should convert the date in the way required by your example.
import java.text.*;
import java.util.*;
public class HelloWorld{
public static void main(String []args) throws ParseException {
String input = "Wed, 18 Feb 2015 22:38:00 +0000";
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
Date date = sdf.parse(input);
SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM d, yyyy");
String dateString = format.format(date);
System.out.println(dateString);
}
}
Related
This question already has answers here:
Java date format - including additional characters
(5 answers)
Closed 2 years ago.
I want to display time as "02 Sep 2020 at 12:24 AM" (mind the at between date and time).
The current format I am using is "dd MMM yyyy hh:mm aaa",
which displays time as "28 Aug 2020 11:32 AM".
How can I put an at before the time?
You can add string literals to a date format by surrounding them with single quotes ('):
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy 'at' hh:mm aaa");
// Here -------------------------------------------------^--^
String formatted = sdf.format(myDateVariable);
If you use java.time for this, you can define a java.time.format.DateTimeFormatter to parse a String, use it to parse the String to a java.time.LocalDateTime and define & use another DateTimeFormatter that includes the at escaping it in the pattern by enclosing it in single-quotes:
public static void main(String[] args) {
String dateTime = "02 Sep 2020 12:24 AM";
DateTimeFormatter parserDtf = DateTimeFormatter.ofPattern("dd MMM uuuu hh:mm a",
Locale.ENGLISH);
DateTimeFormatter outputDtf = DateTimeFormatter.ofPattern("dd MMM uuuu 'at' hh:mm a",
Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(dateTime, parserDtf);
System.out.println(ldt.format(outputDtf));
}
This code produces the output
02 Sep 2020 at 12:24 AM
Just wrap the word in single quotes.
"dd MMM yyyy 'at' hh:mm aaa"
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy 'at' HH:mm:ss z");
Date date = new Date(System.currentTimeMillis());
System.out.println(formatter.format(date));
This question already has answers here:
display Java.util.Date in a specific format
(11 answers)
Closed 5 years ago.
I want to format date to string in "EEE, d MMM YYYY HH:mm:ss z" but not able to receive desired output.
I referred this tutorial however it not working as per expectation , see code below :
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Locale;
public class MyClass {
public static void main(String args[]) {
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM YYYY HH:mm:ss z");
//tried this as well new SimpleDateFormat("EEE',' d MMM YYYY HH:mm:ss z",Locale.US);
String strDate = date.toString();
System.out.println(strDate);// Tue Sep 19 10:31:40 GMT 2017
// i want this --> Tue, Sep 19 2017 10:31:40 GMT
}
}
Please find jdoodle link for working example
Appreciate your help !
You created a DateFormat, but the you didn't use it. Change
String strDate = date.toString();
to
String strDate = formatter.format(date);
I have a time like this : Thu, 27 Oct 2016 18:17:47 GMT
I have tried the code bellow but didn't work
String inputText = "Thu, 27 Oct 2016 18:17:47 GMT";
SimpleDateFormat inputFormat = new SimpleDateFormat
("EEE MMM dd HH:mm:ss 'GMT' yyyy", Locale.US);
inputFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
SimpleDateFormat outputFormat =
new SimpleDateFormat("MMM dd, yyyy h:mm a");
// Adjust locale and zone appropriately
Date date = inputFormat.parse(inputText);
String outputText = outputFormat.format(date);
System.out.println(outputText);
Your input date and input format don't match each other. Try this input format instead:
SimpleDateFormat inputFormat = new SimpleDateFormat
("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
Here are a couple of functions that will do it:
public static Date toLocale(Date date) {
int GMTOffset = (int) TimeUnit.HOURS.convert(calendar.getTimeZone().getRawOffset(),
TimeUnit.MILLISECONDS);
return addHours(date, GMTOffset);
}
public static Date addHours(Date date, int hours) {
calendar.setTime(date);
calendar.add(Calendar.HOUR_OF_DAY, hours);
return calendar.getTime();
}
I want to reformat a given date String into a different format:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE, MMMM dd, yyyy 'at' KK:mm aa zzzz");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = simpleDateFormat.parse(question.getOccur());
String formattedTime = output.format(d);
I'm getting this exception:
java.text.ParseException: Unparseable date: "Monday, December 7, 2015 at 12:05:13 PM Eastern Standard Time" (at offset 33)
You missed the seconds from your date format.
Try this:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE, MMMM dd, yyyy 'at' KK:mm:ss aa zzzz");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = simpleDateFormat.parse(question.getOccur());
String formattedTime = output.format(d);
Edit. For the following example: Thursday 12/10/2015 01:35 AM, the date format is this:
SimpleDateFormat output = new SimpleDateFormat("EEEE dd/MM/yyyy hh:mm a");
This question already has answers here:
Parse any date in Java
(6 answers)
Closed 4 years ago.
I was trying to parse the formatted string date, was getting parse error
input date is "Wed Nov 11 14:24:46 IST 2015", need output date as "Wed Nov 11 2015 14:24:46 IST"
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormat {
public static void main(String[] args) {
try {
String target = "Wed Nov 11 14:24:46 IST 2015";
SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyyy");
Date result = df.parse(target);
SimpleDateFormat df2 = new SimpleDateFormat("EEE MMM dd yyyy kk:mm:ss zzz");
String result2 = df2.format(result);
Date result3 = df.parse(result2);
System.out.println(result2);
System.out.println(result3);
} catch (ParseException pe) {
pe.printStackTrace();
}
}
}
getting error as java.text.ParseException: Unparseable date: "Wed Nov 11 2015 14:24:46 IST"
I have updated my answer to do the parsing as mentioned in your question/comment. See below the explanation:
"Wed Nov 11 14:24:46 IST 2015"
to the following
"Wed Nov 11 2015 14:24:46 IST"
I setup two SimpleDateFormat objects as follow
SimpleDateFormat sourceFormat, destinationFormat;
//this is to format the string to Date object
sourceFormat = new SimpleDateFormat("EEE MMM d kk:mm:ss zzz yyyy", Locale.US);
//this is to format the Date object to desired pattern
destinationFormat = new SimpleDateFormat("EEE MMM d yyyy kk:mm:ss zzz", Locale.US);
I then set the timezone as follow
TimeZone istTimeZone = TimeZone.getTimeZone("Asia/Kolkata");
sourceFormat.setTimeZone(istTimeZone);
destinationFormat.setTimeZone(istTimeZone);
I use the sourceFormat object to format the date string to Date object as follow:
Date sourceDate = sourceFormat.parse(target);
//output: Wed Nov 11 08:54:46 GMT 2015
I then use the destination format to format the date object that represents the string as follow:
Date destinationDate = destinationFormat.format(d);
//output: Wed Nov 11 2015 14:24:46 IST
Basically in order to get a legit Date object I have to use the first SimpleDateFormat sourceFormat which contains pattern that maps the date in String. Once a legit Date object is created using the String then I use the second formatter to re-format the Date object. Below is full code that should give output if copy/pasted.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class ParseDate {
public static void main(String[] args) {
try {
String target = "Wed Nov 11 14:24:46 IST 2015";
SimpleDateFormat sourceFormat, destinationFormat;
sourceFormat = new SimpleDateFormat("EEE MMM d kk:mm:ss zzz yyyy", Locale.US);
destinationFormat = new SimpleDateFormat("EEE MMM d yyyy kk:mm:ss zzz", Locale.US);
TimeZone istTimeZone = TimeZone.getTimeZone("Asia/Kolkata");
sourceFormat.setTimeZone(istTimeZone);
destinationFormat.setTimeZone(istTimeZone);
Date d = sourceFormat.parse(target);
System.out.println(d.toString());
//output: Wed Nov 11 08:54:46 GMT 2015
System.out.println(destinationFormat.format(d));
//output: Wed Nov 11 2015 14:24:46 IST
} catch (ParseException pe) {
pe.printStackTrace();
}
}
}