Convert date formats for storage - java

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

Related

date parsing in java to desire date format

I am trying to do an integration between 2 system,
so there is a datetime field in the source system which has format like
"dd-MMM-yyyy HH:mm:ss" for example- 19-JUN-2017 16:12:30
but what my target system reads is like "yyyy-MM-DD HH:mm:ss"
for example- 2017-12-04 19:14:16
As I am setting a value like
Tableset.setValue("TRANSACTIONDATE",CSVFILECOLUMN[1], 2L);
can any one tell me how can I parse the date format in CSV file here to set the parsed value in my TRANSACTIONDATE field.
Covert your date like this
String mydate="19-JUN-2017 16:12:30";
SimpleDateFormat currentFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
Date d = currentFormat.parse(mydate);
String formattedTime = newFormat.format(d);
then calculate difference

Convert string to a formatted date in Android

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

Conversion of String with timezone to date

I have a string containing date with timezone like
2014-01-24 09.05.14 -06:00.
I need to convert this into date format as YYYYMMDD.
What is the best way in Java to convert the above string into date.
Do it like this (requires JDK 7+):
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss X");
DateFormat outputFormat = new SimpleDateFormat("yyyyMMdd");
String dateString = "2014-01-24 09.05.14 -06:00";
Date date = inputFormat.parse(dateString);
System.out.println(outputFormat.format(date));
One way of doing it is to convert the String into a Date and then use a DateFormatter (e.g. the SimpleDateFormat) to formate that date.
Take a look at this Tutorial for further information.

Converting Date format to other format on Java

I have been developing application with GWT on Java. I used calendar tool for my project on this site. When i select any date, give this format (23-Aug-13) to me.
I use this code:
final DateField txtBirthofDay = new DateField("Birth of Day", "dob", 190);
txtBirthofDay.setValue(new Date());
I want to insert this date to database. So, I have to convert this format (2013-08-23). I converted lots of date format each other before but I didn't convert string type (Aug).
Reference these formats Java Date Format Docs:
try this:
SimpleDateFormat dt = new SimpleDateFormat("dd-MMM-yyyy");
Date date = dt.parse("23-Aug-2013");;
// *** same for the format String below
SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(dt1.format(date));
SimpleDateFormat formats your Date to the given pattern
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(new Date()));
Try this,
DateFormat df=new SimpleDateFormat("dd-MMM-yy");
Date date=df.parse("23-Aug-13");
System.out.println(df.format(date));
df=new SimpleDateFormat("yyyy-MM-dd");
String requiredFormatedDate=df.format(date);
System.out.println(requiredFormatedDate);// 2013-08-23

Date format parsing java

I want to store today's date in the format yyyy-mm-dd. before storing I have took today's date,formatted it and again parse the formatted string to date. It gives the output date in a different format other than what I want. How can i get the date, format it in 'yyyy-mm-dd'
and again convert it into date and want the output in the format 'yyyy-mm-dd'.Please find the below code and tell me where I am wrong
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = new java.util.Date();
java.util.Date date1;
String datestring=dateFormat.format(date);
try {
date1=dateFormat.parse(datestring);
System.out.print(date1);
} catch (ParseException ex) {
Logger.getLogger(accordcclass.class.getName()).log(Level.SEVERE, null, ex);
}
The output for the above code I get is
Thu Mar 07 00:00:00 GMT 2013. But I want the output as 2013-01-07
I had the same problem, this is what I did:
DateFormat inputDateFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm");
DateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
System.out.println(outputDateFormat.format(inputDateFormat.parse("09-SEP-2013 10:00")));
That way I can parse the date in the original format and output it in the database compatible format.
There is the posibility of using PreparedStatement as someone mentiones before but I don't want to.
Don't use a Date object to print, use directly your datestring variable. Using a Date will call toString which will be formatted using the Locale.
Edit : Adding that if you want to store your Date variable with a format, it doesn't work that way. A Date doesn't hold a format, it just represents the time. How you show it in a GUI, console or anywhere else is where you need to specify a format if you want it to differ from the current Locale format.
You're using the DateFormat to format and reparse.
You don't need to re-parse. Simply use the DateFormat only to format.
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = new java.util.Date();
String datestring=dateFormat.format(date);
System.out.println( datestring );

Categories