I am working on Java. I have a String and I want to convert it into Date Format. Please see the lines of code for more details.
String dateString = "4:16:06 PM";
I want to convert it into the below Date:
Date convertedDate = 2014-04-22 16:16:06.00
How can I do this?
try this:
// get current date
SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
String a = dateFormat1.format(new Date());
// add current date to your time string
String dateString = a + " 4:16:06 PM";
SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd h:mm:ss a");
try {
// parse it. This is you date object.
Date d = dateFormat2.parse(dateString);
} catch (ParseException ex) {
ex.printStackTrace();
}
I think you are looking for following:
DateFormat df1 = new SimpleDateFormat("h:mm:ss a");
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SS");
String dateString = "4:16:06 PM";
Date date;
try {
Calendar cal = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
date = df1.parse(dateString);
cal.setTime(date);
// Reset year, month, day to current state
cal.set(Calendar.YEAR, cal2.get(Calendar.YEAR));
cal.set(Calendar.MONTH, cal2.get(Calendar.MONTH));
cal.set(Calendar.DAY_OF_MONTH, cal2.get(Calendar.DAY_OF_MONTH));
Date convertedDate = cal.getTime();
System.out.println(df2.format(convertedDate));
} catch (ParseException e) {
e.printStackTrace();
}
Try this ,
String str_date="13-09-2011";
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd-MM-yyyy");
date = (Date)formatter.parse(str_date);
System.out.println("Date " +date.getTime());
Try this
public class DateClass
{
public static void main(String[] args) throws Exception {
String target = "4:16:06 PM";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
DateFormat dff = new SimpleDateFormat("HH:mm:ss");
Date d=new Date();
String abc="4:16:06 PM";
Date result = new SimpleDateFormat("h:mm:ss a").parse(abc);
System.out.println("The result is "+df.format(d)+" "+dff.format(result));
}
}
Feel free to ask anything if you don't understand it and plz respond did it work or not?
Related
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'SSSX");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("Current date and time in GMT: " + df.format("2017-01-04"));
The above is my code and when I try to convert the date to gmt I get the error:
Exception in thread "main" java.lang.IllegalArgumentException: Cannot
format given Object as a Date at
java.text.DateFormat.format(DateFormat.java:310) at
java.text.Format.format(Format.java:157) at
Sample.main(Sample.java:24)
Please point out what am I doing wrong.
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'SSSX");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("Current date and time in GMT: " + df.format(new Date()));
This above code is working fine but I need something that works for any date.
And when I try this code:
try {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'SSSX");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("Current date and time in GMT: " + df.parse("2017-01-04"));
} catch(Exception e) {
e.printStackTrace();
}
I am getting Unparseable date: "2017-01-04". Please tell me how to fix it
Try it..
String s = "2016-11-21 13:30:0.0";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss);
Date d = null;
try
{
d = format.parse(s);
} catch (ParseException e)
{
e.printStackTrace();
}
SimpleDateFormat gmtFormat = new SimpleDateFormat("yyyy-MM-dd'T'SSSX");
gmtFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(gmtFormat.format(d));
When you have a date object then you can format it as you want:
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
//create a calendar representing your date.
Calendar cal = Calendar.getInstance();
cal.set(2017, 01, 04);
//format the date object to the pattern you want.
String DateToStr = format.format(cal.getTime());
System.out.println(DateToStr);
If you have your date as string you can also do:
String string = "2017-01-04";
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
format1.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = null;
try {
date = format1.parse(string);
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println(date);
The below code is to help in understanding better - you can try uncommenting the commented lines and compare the results. The point of interest for you in this whole block is gdf.parse(gmtFormat)
Date date = new Date();
DateFormat idf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
idf.setTimeZone(TimeZone.getTimeZone("IST"));
DateFormat gdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
gdf.setTimeZone(TimeZone.getTimeZone("GMT"));
//String istFormat = idf.format(date);
//System.out.println("IST: "+istFormat);
String gmtFormat = gdf.format(date);
System.out.println("GMT: "+ gmtFormat);
try {
//System.out.println("gmt parsed from istFormat: "+gdf.parse(istFormat));
//System.out.println("ist parsed from istFormat: "+idf.parse(istFormat));
System.out.println("gmt parsed from gmtFormat: "+gdf.parse(gmtFormat));
System.out.println("ist parsed from gmtFormat: "+idf.parse(gmtFormat));
} catch (ParseException e) {
e.printStackTrace();
}
I have an android application that returns an Entry date Formatted like this 2014-08-26T16:23:30.803
I need it to display 08/16/2014 04:23 pm
Here is my code
items.add(new ListViewItem()
{{
Ticket = json_data.optString("TicketID");
Desc = json_data.getJSONObject("Location").optString("LocationName");
Status = json_data.optString("Status_Desc");
Poster = json_data.optString("Lastposter");
Time = json_data.optString("Entrydate");
}});
public static void main(String args[]) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
SimpleDateFormat sdp = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
System.out.println(sdp.format(sdf.parse("2014-08-26T16:23:30.803")));
}
prints:
08/26/2014 04:23 PM
SimpleDateFormat sd = new SimpleDateFormat("MM'/'dd'/'yyyy hh:mm a");
sd.format(new Date());
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");//dd/MM/yyyy
Date date= new Date("2009-09-22 16:47:08");
String strDate = sdfDate.format(date);
use this
SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy HH:mm a");
String dateInString = " 2014-08-26T16:23:30.803";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
it helps you
I have this code:
String id = c.getString("data");
String name = ((TextView) view.findViewById(R.id.TextView05)).getText().toString();
public static String getDate(long seconds, String dateFormat)
{
DateFormat formatter = new SimpleDateFormat("yyyy MMMM dd HH:mm");
long now = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(now);
return formatter.format(calendar.getTime());
}
The "data" is 1341435600000. I want to have this String from milliseconds to date.
try {
String str_date="11-June-07";
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd-MMM-yy");
date = (Date)formatter.parse(str_date);
System.out.println("Today is " +date );
}
catch (ParseException e)
{
System.out.println("Exception :"+e);
}
for convert milliseconds to Date
long yourmilliseconds = 1119193190;
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
Date resultdate = new Date(yourmilliseconds);
Something like this:
SimpleDateFormat ss = (SimpleDateFormat) SimpleDateFormat.getInstance();
ss.applyPattern("yyyy MM dd HH:mm");
// Date to String:
String dateToString = ss.format(new Date());
//String to Date
Date stringToDate = ss.parse("2012 02 16 14:10"));
String dtStart = "2010-10-15T09:27:37Z";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
Date date = format.parse(dtStart);
System.out.println(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The same method is working fine for me. Why don't yours?
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
long cTime = System.currentTimeMillis();
String date = getDate(cTime, "dd/MM/yyyy hh:mm:ss.SSS");
Toast.makeText(getApplicationContext(), date, Toast.LENGTH_SHORT).show();
}
public static String getDate(long milliSeconds, String dateFormat)
{
// Create a DateFormatter object for displaying date in specified format.
DateFormat formatter = new SimpleDateFormat(dateFormat);
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
if you are not bothered about the Date format, just use it as below
String id = c.getString("data"); //"1341435600000";
Date date123 = new Date(Long.parseLong(id));
System.out.println(date123);
Code snippet to help:
String timestamp="20140809";
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
System.out.println("date :"+dateFormat.parse(timestamp).getTime());
What is the simplest way to convert 23-Mar-2011 to 23-03-2011 in Java?
Thanks everyone. This seem to solve the issue:
try {
Calendar cal = Calendar.getInstance();
cal.setTime(new SimpleDateFormat("dd-MMM-yyyy").parse("24-Nov-2002"));
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
System.out.println(sdf.format(cal.getTime()));
} catch (ParseException e) { e.printStackTrace(); }
Try this
String strDate="23-Mar-2011";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
try {
Date varDate=dateFormat.parse(strDate);
dateFormat=new SimpleDateFormat("dd-MM-yyyy");
System.out.println("Date :"+dateFormat.format(varDate));
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Have you looked at SimpleDateFormat ?
Here's a simpler version of your code:
DateFormat shortFormat = new SimpleDateFormat("dd-MM-yyyy",Locale.ENGLISH);
DateFormat mediumFormat = new SimpleDateFormat("dd-MMM-yyyy",Locale.ENGLISH);
String s = "23-Mar-2011";
String shortDate = shortFormat.format(mediumFormat.parse(s));
System.out.println(shortDate);
SimpleDateFormat format1 = new SimpleDateFormat("dd-MMM-yyyy");
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
Date date = format1.parse("24-Nov-2002");
System.out.println(format2.format(date));
Have a string like 2011-03-09T03:02:10.823Z, how to convert it to Date or calendar object in Java?
You can use SimpleDateFormat#parse() to convert a String in a date format pattern to a Date.
String string = "2011-03-09T03:02:10.823Z";
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
Date date = new SimpleDateFormat(pattern).parse(string);
System.out.println(date); // Wed Mar 09 03:02:10 BOT 2011
For an overview of all pattern characters, read the introductory text of SimpleDateFormat javadoc.
To convert it further to Calendar, just use Calendar#setTime().
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// ...
I want to show "2017-01-11" to "Jan 11" and this is my solution.
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat df_output = new SimpleDateFormat("MMM DD");
Calendar cal=Calendar.getInstance();
Date date = null;
try {
date = df.parse(selectedDate);
String outputDate = df.format(date);
date = df_output.parse(outputDate);
cal.setTime(date);
} catch (ParseException e) {
e.printStackTrace();
}
import java.util.*;
import java.text.*;
public class StringToCalender {
public static void main(String[] args) {
try { String str_date="11-June-07";
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd-MMM-yy");
date = (Date)formatter.parse(str_date);
Calendar cal=Calendar.getInstance();
cal.setTime(date);
System.out.println("Today is " +date );
} catch (ParseException e)
{System.out.println("Exception :"+e); }
}
}
Your given date is taken as a string that is converted into a date type by using the parse() method. The parse() method invokes an object of DateFormat. The setTime(Date date) sets the formatted date into Calendar. This method invokes to Calendar object with the Date object.refer
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date date = sdf.parse(strDate);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
try this.(strDate id your string format of Date)