Convert string to a formated date in java [duplicate] - java

This question already has answers here:
Change date format in a Java string
(22 answers)
Closed 9 years ago.
Hi, I have a string like:
String selectedDate = "Fri Jan 17 00:00:00 CAT 2014";
I need to format it to:
DD.MM.YYYY = 17.01.2014 00:00:00
Can anyone help please see code below:
String date = getDate(selectedDate);
private String getDate(String inpuDate) {
Date date = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss ").parse(inputDate);
return new SimpleDateFormat("dd.mm.yyyy hh:mm:ss").format(date);
}

Try the following:
String date = getDate(selectedDate);
private String getDate(String inpuDate) {
Date date = new SimpleDateFormat("EEE MMM d hh:mm:ss z yyyy").parse(inputDate);
return new SimpleDateFormat("dd.MM.yyyy hh:mm:ss").format(date);
}

Related

ParseException: Unparseable date in Java [duplicate]

This question already has answers here:
Java - Unparseable date
(3 answers)
java DateTimeFormatterBuilder fails on testtime [duplicate]
(2 answers)
Closed 3 years ago.
I need to convert String (in date format) to TimeStamp.
IN first I did like this.
private Timestamp dateConverter(String date) {
Timestamp timestamp = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm");
Date parsedDate = null;
try {
parsedDate = dateFormat.parse(date);
timestamp = new java.sql.Timestamp(parsedDate.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return timestamp;
}
Result was: ParseException: Unparseable date: "16 Jan 2020 11:02"
I tried by another way :
private Timestamp dateConverter(String date) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("dd MMM yyyy HH:mm");
LocalDateTime ldt = LocalDateTime.parse(date , f);
return Timestamp.valueOf(ldt);
}
At this time I have Internal Server Error with DateTimeParseException: Text '16 Jan 2020 11:02' could not be parsed at index 3
Your pattern can't know what is the language of your month, English, French, Arabic, Russian, Mandarin..., to solve this you have to help the formatter by using Locale like so :
DateTimeFormatter f = DateTimeFormatter.ofPattern("dd MMM yyyy HH:mm")
.withLocale(new Locale("us"));
LocalDateTime ldt = LocalDateTime.parse("16 Jan 2020 11:02" , f); // 2020-01-16T11:02

Converting Java string to Date [duplicate]

This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Closed 4 years ago.
I need to convert a java string in a date format to a new date format to send it to my sql-server. Currently, my Java date is Jul 17 2018 14:22:58, and I need it to look like 2018-07-17 14:22:58. So I created this:
String oldTime = "Jul 17 2018 14:22:58";
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD HH:MM:SS");
Date formattedDate = sdf.parse(oldTime);
I am getting errors that my oldTime is unparseable. Did I do this wrong?
You need to use a format string that matches your date string:
String oldTime = "Jul 17 2018 14:22:58";
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy HH:mm:ss");

How to get clock from a date [duplicate]

This question already has answers here:
How to format date and time in Android?
(26 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I have Date like this Sun May 20 09:18:44 GMT+04:30 2018 how i can get clock from this Date,
i want result like this 09:18 am
Log.v(TAG,"date "+ date.toString());// output is Sun May 20 09:18:44 GMT+04:30 2018
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm");
String time=sdf.format(date);//NullPointerException
your code is working you just have to initialize Date class's object
Date date = new Date();
Date date = new Date();
Log.v(TAG,"date "+ date.toString());
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm");
String time=sdf.format(date);
Log.v(TAG,"date "+ time);
Try this code..
SimpleDateFormat sdf3 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
SimpleDateFormat sdf4 = new SimpleDateFormat("hh:mm", Locale.ENGLISH);
Date d1 = null;
try{
d1 = sdf3.parse("Sun May 20 09:18:44 GMT+04:30 2018");
String date=sdf4.format(d1);
System.out.println("time..." + date);
}catch (Exception e){ e.printStackTrace(); }
System.out.println("check..." + d1);

How to count down 6 month in android? [duplicate]

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

Convert String to Date [duplicate]

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));

Categories

Resources