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!
Related
This question already has answers here:
12:xx shown as 00:xx in SimpleDateFormat.format("hh:mm:ss")
(1 answer)
Difference between java HH:mm and hh:mm on SimpleDateFormat
(5 answers)
Closed 1 year ago.
public static String getTime(String time) {
String ampmTime = null;
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss", Locale.getDefault());
Date dt;
try {
dt = sdf.parse(time);
SimpleDateFormat sdfs = new SimpleDateFormat("hh:mm a", Locale.getDefault());
if (dt != null) {
ampmTime = sdfs.format(dt);
}
} catch (ParseException e) {
e.printStackTrace();
}
return ampmTime;
}
In parameter string i'm sending 12:00:00 format so please i want this as 12:00pm instead of 12:00am . And others time is showing correctly like 13:30:00 to 1:30pm . So please help me??
As stated here, When using hh you are telling the formatter the input time is an am/pm time. Use HH for 24hrs input.
Change the format to:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
This question already has answers here:
SimpleDateFormat producing wrong date time when parsing "YYYY-MM-dd HH:mm"
(5 answers)
Closed 5 years ago.
I was trying to convert date from yyyy-MM-dd format to yyyy-MM format, when I run the below code with the input "2012-10-22" it is giving me an output of 2012-JAN instead of 2012-OCT. any thoughts on where I am doing wrong?
public static String dateFormatter(String presentDate)
{
String formattedDate = "";
SimpleDateFormat tempFormat = new SimpleDateFormat("YYYY-MM-DD");
SimpleDateFormat finalFormat = new SimpleDateFormat("YYYY-MMM");
try {
Date currentFormat = tempFormat.parse(presentDate);
formattedDate = finalFormat.format(currentFormat);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return formattedDate;
}
Change the first format to
SimpleDateFormat tempFormat = new SimpleDateFormat("yyyy-MM-dd");
as DD is the day in the year. 22 is definitely in January
Use this
SimpleDateFormat tempFormat = new SimpleDateFormat("yyyy-MM-dd");
Instead of
SimpleDateFormat tempFormat = new SimpleDateFormat("YYYY-MM-DD");
This question already has answers here:
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Closed 5 years ago.
I am trying to parse string into a date using the following code:
public static Date dateFormatter(String s)
{
SimpleDateFormat ft = new SimpleDateFormat ("MMddYYYY");
Date excelDate=null;
try
{
excelDate = ft.parse(s);
Date formatString = ft.format(excelDate);
System.out.println("Date to be printed in Excel is :" +formatString);
return excelDate;
}
catch(Exception ae)
{
System.out.println("No date");
}
return excelDate;
}
I am passing in the argument "04202017".
This function is not working for me. I am not able to figure out what I am doing wrong. Can anybody please help me?
You have to use ft.parse(s); instead of format(excelDate). Format is the other way (Date -> String)
DateFormat.parse(String)
And you dont have to parse the Date back to a String.
Corrected code:
public static Date dateFormatter(String s) {
SimpleDateFormat ft = new SimpleDateFormat ("MMddYYYY");
Date excelDate = null;
try {
excelDate = ft.parse(s);
System.out.println("Date to be printed in Excel is :" +excelDate);
return excelDate;
} catch(Exception ae) {
System.out.println("No date");
}
return excelDate;
}
You already parsed String s to excelDate with date format that you want. So i think it's good and enough to print just excelDate.
System.out.println("Date to be printed in Excel is :" +excelDate);
Like that.
And also change MMddYYYY to MMddyyyy.
Try parse method instead of format
For String to Date, use:
SimpleDateFormat.parse(String);
For Date to String, use:
SimpleDateFormat.format(date);
However, in your code, you already parsed the String and assigned into excelDate on this line:
excelDate = ft.parse(s);
try this one:
String string = "march 9, 2017";
DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);
It would be nice to use Java 1.8's new time classes (which are in java.time.* package).
public static void main(String[] args)
{
LocalDateTime dateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// To String
String dateString = dateTime.format(formatter);
System.out.println(dateString);
// To LocalDateTime
LocalDateTime parsedLocalDateTime = LocalDateTime.parse(dateString, formatter);
}
This question already has answers here:
How to format date and time in Android?
(26 answers)
Closed 7 years ago.
I have to display the date in dd-mm-yy format as per the UI design for our Android app. can any one help me, how to achieve it?
Use SimpleDateFormat:
http://developer.android.com/reference/java/text/SimpleDateFormat.html
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy");
Log.i("DATE", sdf.format(new Date()));
You should store your instance of sdf, if you are planning to be formatting your dates repeatedly. Outside a loop, or as a class member variable, for example.
You can done it by SimpleDateFormat
For Example,
android.text.format.DateFormat dateformat = new android.text.format.DateFormat();
dateformat.format("MM-dd-yyyy hh:mm:ss", new java.util.Date());
or
android.text.format.DateFormat.format("MM-dd-yyyy hh:mm:ss", new java.util.Date());
You can change your date object to MM-ddy-yy using SimpleDateFormat. You can create a helper method like the following
public String formatDate(Date date) {
DateFormat formatter = new SimpleDateFormat("MM-dd-yy");
String formattedDate = formatter.format(date);
return formattedDate;
}
try this code , it may help you
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String abs= m3_DateDisplay.getText().toString();
Date testDate = null;
try {
testDate = sdf.parse(abs);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy");
String dateValue=formatter.format(testDate);
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));
}
}