This question already has answers here:
Java string to date conversion
(17 answers)
Change date format in a Java string
(22 answers)
Closed 6 years ago.
Wanted to convert the following dates format:
Mar 10th 2016
Mar 1st 2016
Mar 2nd 2016
Mar 3rd 2016
Mar 22nd 2016
into
10-03-2016
01-03-2016
02-03-2016
03-03-2016
22-03-2016
Tried couple of things but failed to get the desired output.
Try string manipulation. Read the input, split based on space and write a logic for your own converter
Try this :)
private String converter (String in) throws Exception {
String stringMonth = in.split(" ")[0];
Date date = new SimpleDateFormat("MMM", Locale.ENGLISH).parse(stringMonth);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH);
int day = Integer.valueOf(in.split(" ")[1].replaceAll("[a-z]", ""));
int year = Integer.valueOf(in.split(" ")[2]) - 1900;
Date result = new Date();
result.setYear(year);
result.setMonth(month);
result.setDate(day);
DateTime dt = new DateTime(result);
return dt.toString("dd-MM-yyyy");
}
As there is no pattern in SimpleDateFormat for ordinal day numbers, you first should clean the string, then parse the date and stringify it back using two SimpleDateFormats:
static final DateFormat DF_FROM = new SimpleDateFormat("MMM d yyyy", Locale.ENGLISH);
static final DateFormat DF_TO = new SimpleDateFormat("dd-MM-yyyy");
synchronized static String convert(String date) throws ParseException {
date = date.replaceAll("(?<=\\d)(st|nd|rd|th)", ""); // remove ordinal suffix
return DF_TO.format(DF_FROM.parse(date)); // convert
}
Please note usage of synchronized for method as SimpleDateFormat methods are not thread-safe.
Related
This question already has answers here:
How to convert Date to a particular format in android?
(7 answers)
Converting date-string to a different format
(5 answers)
Changing String date format
(4 answers)
Closed 3 years ago.
I'm having a string like this: Wed Feb 20 02:48:00 GMT+05:30 2019
and I need to convert it to Wed 20.
How can I do this?
I tried string.replace(), but that doesn't help
I'm a newbie please help
If you will be working with dates that are always represented in this String format,
then perhaps you can use the String split method, to break apart your date on the spaces.
String sourceDate = "Wed Feb 20 02:48:00 GMT+05:30 2019";
String[] dateParts = sourceDate.split(" ");
This will result in an Array containing the seperate blocks.
dateParts = { 'Wed', 'Feb', '20','02:48:00', 'GMT+05:30', '2019' }
Then you can take the blocks you need
String resultDate = dateParts[0] +" "+ dateParts[2];
Wed 20
If you intend to do other manipulations on the date, you might want to look into converting your date String to an actual Date using a DateTimeFormatter
This works like this:
String string = "January 2, 2010";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy", Locale.ENGLISH);
LocalDate date = LocalDate.parse(string, formatter);
System.out.println(date); // 2010-01-02
Where LocalDate.parse() needs to be replaced depending on your date format.
Only the date -> LocalDate#parse(text, formatter)
Date and time -> LocalDateTime#parse(text, formatter)
Date, time and timezone -> ZonedDateTime#parse(text, formatter)
try this
class Test
{
public static void main(String ... args)
{
String s="Wed Feb 20 02:48:00 GMT+05:30 2019";
String arr[]=s.split(" ");
if (arr.length >=3)
{
String result=arr[0]+" "+arr[2];
System.out.println(result);
}
}
}
$ java Test
Wed 20
This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 5 years ago.
I want to change date format in JAVA as in below. Please help.
-> input is of java.util date datatype (Sat Jan 20 00:00:00 IST 2018)
EX-
Calendar c = Calendar.getInstance();
Date input = c.getTime();
System.out.println(input);//prints[Sat Jan 20 00:00:00 IST 2018]
-> Output should be of java.util date datatype (2018-01-20)
EX-
Date output = null;
output = **[logic to convert input to 2018-01-20]**
System.out.print(output);//should print 2018-01-20
I am getting output in String format.
Please help me to find the output in Date format
You will need two SimpleDateFormat objects. One for parsing the input and one for formatting the output.
public static void main(String[] args) throws ParseException {
String input = "Tue Dec 20 00:00:00 IST 2005";
SimpleDateFormat inFormat = new SimpleDateFormat("EEE MMM d H:m:s zzz y");
Date d = inFormat.parse(input);
System.out.println("d = " + d);
SimpleDateFormat outFormat = new SimpleDateFormat("y-MM-d");//(2005-12-20
System.out.println("" + outFormat.format(d));
}
For a more indepth description on how to format the SimpleDateFormat object, check out the Javadocs.
This question already has answers here:
Illegal pattern character 'T' when parsing a date string to java.util.Date
(4 answers)
Closed 5 years ago.
I have specific case that I must take the date field, convert it to GMT time and then to convert it to specific String format.
This gives the GMT time:
public static void main(String[] args) {
Date rightNow = Calendar.getInstance().getTime();
DateFormat gmtFormat = new SimpleDateFormat();
TimeZone gmtTime = TimeZone.getTimeZone("GMT");
gmtFormat.setTimeZone(gmtTime);
System.out.println("GMT Time: " + gmtFormat.format(rightNow));
String gmtDate=gmtFormat.format(rightNow);
}
Now I need to that GMT time convert to String format yyyy-MM-ddTHH:mm:ssZ
Example current time in my time zone is 17:10:00, in GMT 15:10:00 so it means final output should be 2017-08-07T15:10:00Z
I tried this code to add:
String pattern = "yyyy-MM-ddTHH:mm:ssZ";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(gmtDate);
System.out.println(date);
But of course I am getting the exception because string cannot be converted like this, but I need something similar.
Merge your 2 codeblocks together:
public static void main(String[] args) {
Date rightNow = Calendar.getInstance().getTime();
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
DateFormat gmtFormat = new SimpleDateFormat(pattern);
TimeZone gmtTime = TimeZone.getTimeZone("GMT");
gmtFormat.setTimeZone(gmtTime);
System.out.println("GMT Time: " + gmtFormat.format(rightNow));
}
Or "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" as per JavaDoc...
This question already has answers here:
android java parse date from string
(3 answers)
Closed 5 years ago.
I have a date string in this format '20161201'.
How to make this a datetime string to be july 2016?
I want to show 6 months before that datetime string.
Is it possible?
Also how do I convert just '20161201' to Dec 2016?
you change the format using the following code,
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
SimpleDateFormat expectedSDF = new SimpleDateFormat("MMMM yyyy", Locale.getDefault());
Date date = simpleDateFormat.parse(dateString);
String newFormatString = expectedSDF.format(date);
And for the countdown, use CountDownTimer
https://developer.android.com/reference/android/os/CountDownTimer.html
For convert '20161201' to 'Dec 2016':
//Convert string to date
String dateString = "20161201";
SimpleDateFormat parseDateFormat = new SimpleDateFormat("yyyyMMdd");
Date convertedDate = new Date();
try {
convertedDate = parseDateFormat.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
//Convert date to string
SimpleDateFormat outputDateFormat = new SimpleDateFormat("MMM yyyy");
String result = outputDateFormat.format(convertedDate);
//Print the result
System.out.println(result);
In '6 months' to millisecond is 15778476000. You can get the date after 6 months in millisecond by using
long resultdate = convertedDate.getTime() - 15778476000L;
and then
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(resultdate);
String result = outputDateFormat.format(calendar.getTime());
to get the result
https://developer.android.com/reference/java/text/SimpleDateFormat.html
https://developer.android.com/reference/java/util/Date.html
https://developer.android.com/reference/java/util/Calendar.html#setTimeInMillis(long)
how to make this datetime string to be july 2016
I want to show 6 months before that datetime string
July is 5 months...
Here's the 6 month answer
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(new SimpleDateFormat("yyyyMMdd").parse("20161201"));
c.set(Calendar.MONTH, c.get(Calendar.MONTH)-6);
String output = new SimpleDateFormat("MMMM yyyy").format(c.getTime()));
// June 2016
This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 7 years ago.
From this question and its answers, I tried to convert string to date. But it seems to strange with me.
String test = "2015/01/01 11:56:00 ";
SimpleDateFormat df = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss");
System.out.println(df.parse(test));
It returns
Mon Dec 29 11:56:00 ICT 2014
I have tried with other days but the results is not in the rule (i.e. it have the same distance from the input string and the output date). I am curious. Can anyone explain this for me?
First of all its yyyy not YYYY for year and try the below code to create a Date object from a String.
String test = "2015/01/01 11:56:00 ";
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = df.parse(test);
System.out.println(date);
The date format is case-sensitive and therefore the parsing is evaluated differently from what you expected.
Try this:
String test = "2015/01/01 11:56:00 ";
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println(df.parse(test));