In my Java Cloud Endpoints API I have some code to get the current date and then store that date in my Cloud SQL (MySQL) database:
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String formattedDate = df.format(c.getTime());
//formattedDate is then inserted into the database
Later on in my Android Activity I query the database and get the date back as a String that looks like:
2015-06-24 17:53:01
Now I want to format this date to display it like 06/24/2015 on the UI of my Activity. To accomplish this I do the following:
//I get a string like 2015-06-24 17:53:01 passed in from another activity
//which in-turn got it from the MySQL database
datetime = getIntent().getExtras().getString("datetime");
//first convert the string datatime to a date object
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(datetime);
} catch (ParseException e) {
e.printStackTrace();
}
//then format that date object the way you want
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
String formattedDate = df.format(convertedDate);
//set the TextView in my Activity to display 06/24/2015
myDateTextView.setText(formattedDate);
This works. But man is it a convoluted way to do something simple. I am wondering if there is a more efficient way to do this?
You should save long value represented date into DB.
long l = c.getTimeInMillis()
and this value you should save.
answer on your question below:
Date date=new Date(l);
SimpleDateFormat df2 = new SimpleDateFormat("MM/dd/yyyy");
String dateText = df2.format(date);
System.out.println(dateText);
The simplest way is to store date in long value at DB, after that you can format to string in a very simple way.
DateFormat.format("MMM dd, yyyy", milliseconds).toString();
If you can change the source code of insertion into database, try inserting the timestamp in Long. Then conversion of timestamp to date format should be straight forward.
in Android,
final String data = DateFormat.format("MM/dd/yyyy",timestamp);
Related
I want to enter the current date and time along with second in a text box ; How can I do that ?
The code what I have written is as below , but it is only entering the string value what I am passing in send keys , which is clearly indicating I am not in correct track .
Any suggestion ?
// Create object of SimpleDateFormat class and decide the format
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy ");
//get current date time with Date()
Date date = new Date();
// Now format the date
String date1= dateFormat.format(date);
// Print the Date
System.out.println(date1);
collection_title.sendKeys("date1");
In collection_title.sendKeys("date1"); you are not using your variable date1, you are using the String "date1" - there should be no quotes around date1.
It should work like this:
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = new Date();
collection_title.sendKeys(dateFormat.format(date));
Maybe you should try to use LocalDateTime.now() or Calender.getInstance() instead of new Date(), because the Date API methods are flawed and deprecated.
I am trying to in insert date into sqlite using:
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String data = format.format(new Date(19900101));
System.err.println("Time is.."+format.format(19900101));
But i am getting 19700101 in log. i am getting same result even if i change the dates as 20000101 or any thing.
Any one suggest me why it happens?
the code is wrong
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Date data = format.parse("19900101");
System.out.println("Time is.."+data );
when u use new Date(123) it expects some time in milliseconds i think
Change your code as for formatting current string to date :
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Date convertedDate = (Date) format.parse("19900101");
SimpleDateFormat formats = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String fmconvertedDate =formats.format(convertedDate);
System.err.println("Time is.."+fmconvertedDate);
and output is :
Time is..1990-01-01 00:00:00
Having some troubles and can't find a quick answer..
Im trying to store a date within a string, and later fetch it to convert it back to a date.
However when storing the date using:
string tmp = new Date().toString();
And then trying to convert it back using
Date date = new Date(tmp);
I get the Exception type
java.lang.IllegalArgumentException
with my Android 2.2 device. Does work with 2.2 & 2.3 emus tho.
Any tips on what other way i can store and convert back?
You could use SimpleDateFormat with its methods parse() and format().
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS");
String tmp = sdf.format(new Date());
Date date = sdf.parse(tmp);
Do you need it to be a string? long is easier :)
do
long time = new Date().getTime();
Date date = new Date(time);
then you dont' have to parse
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
// to string
String dateStr = formatter.format(new Date());
// to date
Date date = formatter.parse(dateStr);
use SimpleDateFormat as shown below.
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
//this will convert date into string.
String temp = formatter.format(currentDate.getTime());
//this will convert string into date format.
Date date=(Date)formatter.parse(temp);
All of my code formats and displays dates as this:
3-1-2011
3-15-2011
12-1-2011
12-15-2011
What's the best way to format that as YYYY-MM-DD for storage in an sqlite db?
Try this
String inputDate = "12-15-2011";
DateFormat df = new SimpleDateFormat("YYYY-MM-DD");
Date date = df.parse(inputDate);
HI,
I am converting String to Date format. But it returns wrong dates. for example,
String startDate = "08-05-2010"; // (MM/dd/yyyy)
I want to convert this to "Date" object like this, 05-JUL-10
How to do that? I tried like this
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy");
scal1.setTime(dateFormat.parse((startDate)));
but i am getting "Unparseable date:" .
If you want to convert a date string of one format to another format, you can use format() and parse() methods of SimpleDateFormat class
First you need to parse the string to a date object using parse() method setting the source pattern and then format the date object using format() method setting the target pattern:
SimpleDateFormat sourceFormat = new SimpleDateFormat("MM-dd-yyyy");
Date sourceFormatDate = sourceFormat.parse("08-05-2010");
SimpleDateFormat destFormat = new SimpleDateFormat("dd-MMM-yy");
String destFormatDateString = destFormat.format(sourceFormatDate);
System.out.println(destFormatDateString); // 05-Aug-10
Unless you've left something out, it looks like you're trying to parse it with the wrong format, i.e. you have an mm-dd-yyyy, and you're trying to parse it with the format dd-MMM-yy. Try a separate date format for what you're parsing from what you're encoding.
SimpleDateFormat format = new SimpleDateFormat(“yyyy-MM-dd”);
String strDate = “2007-12-25″;
Date date = null;
try {
date = format.parse(strDate);
} catch (ParseException ex) {
ex.printStackTrace();
}
The format dd-MMM-yy you use to parse the string is wrong; the format should be dd-MM-yyyy.
String startDate = "08-05-2010";
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = dateFormat.parse((startDate));
Note that Date objects themselves do not have a format: a Date objects just represents a date and time value, just like an int is just a number, and doesn't have any inherent formatting information. If you want to display the Date in a certain format, you'll have to use a DateFormat object again to format it:
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy");
System.out.println(dateFormat.format(date));
// output: 08-May-10