Java SimpleDateFormat not working [duplicate] - java

This question already has answers here:
How to format date and time in Android?
(26 answers)
Closed 4 years ago.
I am displaying the date and time in Android with this format:
2015-11-25 23:25:00
How can I change it to the following format?
2015-11-25
I wrote some code ,but not working correctly.
public static Date toDate(boolean isCurrentDate, String dateString) {
Date date=null;
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
try {
if (isCurrentDate)
date = new Date();
else
date = formatter.parse(dateString);
System.err.println("Printresult" + formatter.parse(formatter.format(date)));
} catch (ParseException e1) {
e1.printStackTrace();
}
System.err.println("Printresult2" + date.toString());
return date;
}
I log and result is like this
Wed Nov 25 00:00:00 GMT+00:00 2015
How i can change date format like this : ? (2015-11-25)

You need one format for parsing and another for printing if the formats differ.
The following code deals with displaying the date in the format you want.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(date));
You could alter the beheviour of toString() method by using anonymous class and overrriding toString() method or just create named class deriving from Date, but there is no point to do it.

Related

Convert string to Date and compair it to Current date [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 5 years ago.
Hello Guys This problem is driving me crazy right now. I have a string in my database called dueDate with value Jan 18, 2018. I retrieved it and save it in a string in my recyclerAdapter with String dueDate = transactionTasks.get(position).get_transactiontaskpaydet().toString();
Now i wanna compare it with the current date and if the due date is after my current date it should display the dueDate with red color.
bellow are my coded.
String dueDate = transactionTasks.get(position).get_transactiontaskpaydet().toString();
Date cdate = new Date();
Date ddate = new Date(dueDate);
if(cdate.after(ddate)){
holder.date.setTextColor(Color.RED);
}
This codes work perfectly but the problem is Date(dueDate); is deprecated. And when i use another method that uses a try and catch, i don't get any result.
Below are the codes
SimpleDateFormat format = new SimpleDateFormat("E, MMM dd yyyy");
Date curDate = new Date();
try {
Date datedue = format.parse(dueDate);
if(curDate.after(datedue)) {
holder.date.setTextColor(Color.RED);
}
} catch (ParseException e) {
e.printStackTrace();
}
And if I try creating the Date outside the try block by using Date duedate = null; I get an error because its not getting the values inside the try block. this is driving me crazy because I shouldn't use deprecated code even if it works perfectly.
All the answers I found didn't work for me. I just need to be able to convert a string to a date so I can compare it with the current date. Thanks Guys
I think the issue is with your SimpleDateFormat. If your String really is "Jan 18, 2018" then you need your SimpleDateFormat to match that like this:
SimpleDateFormat format = new SimpleDateFormat("MMM dd, yyyy");
I made a quick method to test this and it worked fine:
public static void main(String args[]) {
String dueDate = "Jan 18, 2018";
SimpleDateFormat format = new SimpleDateFormat("MMM dd, yyyy");
Date curDate = new Date();
try {
Date datedue = format.parse(dueDate);
if(curDate.after(datedue)) {
holder.date.setTextColor(Color.RED);
}
} catch (Exception e) {
e.printStackTrace();
}
}
I hope this helps!

Is there any way of converting time from "hh:mm:ss" format to "hh:mm AM/PM" format? [duplicate]

This question already has answers here:
Conversion from 12 hours time to 24 hours time in java
(17 answers)
Closed 6 years ago.
I am storing time in the database in this format: "hh:mm:ss" (example- 09:30:00) and then retrieving and trying to show it to users in this format: "hh:mm AM/PM" (example- 09:30 AM).
I'm using below code for converting it:
DateFormat currentTime = new SimpleDateFormat("hh:mm a");
String startTimeInSF = currentTime.format(startTime);
String endTimeInSF = currentTime.format(endTime);
where startTime and endTime is in hh:mm:ss format, but the above code is producing this error: java.lang.IllegalArgumentException: Bad class: class java.lang.String.
Please let me know how can I successfully convert the time from hh:mm:ss to hh:mm AM/PM?
I think you should parse your "hh:mm:ss" time into a Date Object, then use formatter to format it to "hh:mm a".
Like this :
DateFormat format1 = new SimpleDateFormat("hh:mm:ss");
try {
Date date = format1.parse("01:11:22");
SimpleDateFormat format2 = new SimpleDateFormat("hh:mm a");
String result = format2.format(date);
return result;
} catch (ParseException e) {
e.printStackTrace();
}
I believe you're looking for the parse(String source) method. The format() methods take in a Date object and output a String representation of the object. the parse methods take in a String object and converts it to a Date object.
To do what you want, you'll need to have a DateFormat with hh:mm:ss, convert the database String to a Date using parse, and then use your existing DateFormat and use format on the Date object to get the output String to display to your user.
http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html
You need to change your code something like this, format function will not work directly on String object that is root cause of your exception.
DateFormat inputFormatter1 = new SimpleDateFormat"HH:mm:ss");
Date date1 = inputFormatter1.parse("22:10:11");
DateFormat outputFormatter1 = new SimpleDateFormat("hh:mm a");
String output1 = outputFormatter1.format(date1); //
Out will be 10:10 pm

Android convert UTC Date to local timezone [duplicate]

This question already has answers here:
java date parse exception while conveting UTC to local time zone
(5 answers)
Closed 7 years ago.
I get this date string from my API : "2015-12-07T14:11:15.596Z"
But this date is in UTC format and I want to convert it in local time, how can I do it ?
I tried this :
try
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return simpleDateFormat.parse(this.created_at);
}
catch (ParseException e)
{
Log.e("Error Date at Whisp", e.getMessage());
return null;
}
But it return me this error :
Unparseable date: "2015-12-07T13:21:17.996Z" (at offset 10)
your Date Format pattern is wrong. Change to:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
For more informations see the javadoc of SimpleDateFormat
The T and the Z are not in your mask
Either
created_at = created_at.replace ("T", "").replace ("Z", "");
or modifiy your mask

conversion date from string bring wrong value [duplicate]

This question already has answers here:
How to convert a String to a Date using SimpleDateFormat?
(10 answers)
Closed 9 years ago.
I'm using the following conversion from string to date
String attr = "2013-09-11"
Date date = null;
SimpleDateFormat parsedDate = null;
parsedDate = new SimpleDateFormat("yyyy-mm-dd");
try {
date = parsedDate.parse(attr);
} catch (ParseException e) {
}
Now the date have value Fri Jan 11 00:09:00 IST 2013
why is that? and how can I change it to bring appropriate value?
mm is for minutes, you're looking for MM for months.
parsedDate = new SimpleDateFormat("yyyy-MM-dd");
More info: SimpleDateFormat, in the Date and Time Patterns and Examples section .

Convert string "11-10-10 12:00:00" into Date object [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to parse date in java?
I want to convert the string "11-10-10 12:00:00" into a Date object, but I am not able to do so. Can you please help me out?
I have the Date object which has the value "Mon Oct 11 00:00:00 IST 2010"
DateFormat newDateFormat = new SimpleDateFormat("dd-MM-yy hh:mm:ss");
String strDate = newDateFormat.format(tempDate);
//**i got strDate as strDate is : 11-10-10 12:00:00**
DateFormat newDateFormat1 = new SimpleDateFormat("dd-MM-yy hh:mm:ss");
try {
tempDate = newDateFormat1.parse(strDate);
// **getting tempDate as - Mon Oct 11 00:00:00 IST 2010**
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DateFormat newDateFormat = new SimpleDateFormat("dd-MM-yy HH:mm:ss");
Date d = newDateFormat.parse("11-10-10 12:00:00");
System.out.println(d);
Here is ideone demo
I think this is Java code—not my specialty—but I think your issue is the "hh" in your format string, which causes parse to interpret "12:00:00" as midnight instead of noon. Try changing it to "HH" and see if that parses correctly.
You need to use CultureInfo for Hindi it is "hi-IN". For full list of cultures check this link CultureInfo
class Program
{
static void Main(string[] args)
{
var str = "11-10-10 12:00:00";
DateTime dateTime = DateTime.Parse(str, new CultureInfo("hi-IN", false));
}
}

Categories