String RollStart_TIMESTAMP = RollStartDATE_TIME_STAMP+" "+rolStart_TIME_STAMP;
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-mm-yyyy hh:mm");
Date parsedDate = null;
try {
parsedDate = dateFormat.parse(RollStart_TIMESTAMP);
long mili = parsedDate.getTime();
Log.e("VX:","TIMESTAMP"+mili);
} catch (ParseException e) {
e.printStackTrace();
}
Log.e("VX:","TIMESTAMP"+parsedDate);
Try this code, You get a right time stamp.
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM dd,yyyy HH:mm aaa");
timestamp = dateFormat.format(new Date()); // Find todays date
Log.e("Timestamp", timestamp);
Yes right there is wrong pattern yyyy-MM-hh is helps me out .
try {
String RollStart_TIMESTAMP = RollStartDATE_TIME_STAMP+" "+rolStart_TIME_STAMP;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date timestamp = null;
timestamp = df.parse(RollStart_TIMESTAMP);
long ROLL_START_DATE_TIME_VALUE = timestamp.getTime()/1000;
Log.e("RX:","VALUE"+ROLL_START_DATE_TIME_VALUE);
} catch (Exception e) {
e.printStackTrace();
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();
}
How can i convert 20160729161455 to 29/07/2016 16:14:55?
I've tried the following but it did not work:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String dateID = 20160729161455;
try
{
Date date = sdf.parse(dateID);
System.out.println(date);
} catch (ParseException ex){}
Yes you can do. You need 2 SimpeDateFormat objects:
SimpleDateFormat dateParser= new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String dateID = "20160729161455";
try
{
Date date = dateParser.parse(dateID);
System.out.println(dateFormatter.format(date));
} catch (ParseException ex){
ex.printStacktrace();
}
In first step you have to parse the value and in second you have to format it in the new format.
I need help parsing this string of data and time. Could any one help me fix my format or give guidance? Thanks.
2016-04-23T01:00:00Z
SimpleDateFormat year = new SimpleDateFormat("mm:dd:yyyy");
SimpleDateFormat hour = new SimpleDateFormat("hh:mm:ss");
String yearParse = year.format(Date.parse(year));
String hourParse = hour.format(Date.parse(hour));
Use this methods to get Date and Time in format you wants
For Date
public static String getDate(String date) {
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
sourceFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date parsed = null;
try {
parsed = sourceFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy");
return formatter.format(parsed);
}
For Time
public static String getTime(String date) {
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
sourceFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date parsed = null;
try {
parsed = sourceFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
return formatter.format(parsed);
}
Try
SimpleDateFormat sdf1= new SimpleDateFormat("mm-dd-yyyy");
SimpleDateFormat sdf2= new SimpleDateFormat("hh-mm-ss");
String year=sdf1.format(required_date);
String hour=sdf2.format(required_date);
I am getting problem in converting a Date into milliseconds
try {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd, HH:mm:ss");
formatter.setLenient(false);
String oldTime = "2013-06-11 06:40:24";
Date oldDate;
oldDate = formatter.parse(oldTime);
Sysout(oldDate.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
It is throwing a Parsing Exception.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd, HH:mm:ss");
should be
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Also, I think you should write :
oldDate = formatter.parse(oldTime);
I'm trying to parse a string to a date field in an android application but I can't seem to get it correct. Here is the string I'm trying to convert to a date "03/26/2012 11:49:00 AM". The function I'm using is:
private Date ConvertToDate(String dateString){
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return convertedDate;
}
But I keep getting 3/1/112 11:49AM as the result.
You are wrong in the way you display the data I guess, because for me:
String dateString = "03/26/2012 11:49:00 AM";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(convertedDate);
Prints:
Mon Mar 26 11:49:00 EEST 2012
it went OK when i used Locale.US parametre in SimpleDateFormat
String dateString = "15 May 2013 17:38:34 +0300";
System.out.println(dateString);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss Z", Locale.US);
DateFormat targetFormat = new SimpleDateFormat("dd MMM yyyy HH:mm", Locale.getDefault());
String formattedDate = null;
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(dateString);
System.out.println(dateString);
formattedDate = targetFormat.format(convertedDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(convertedDate);
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("Today is " +date.getTime());
Try this
This code will help you to make a result like FEB 17 20:49 .
String myTimestamp="2014/02/17 20:49";
SimpleDateFormat form = new SimpleDateFormat("yyyy/MM/dd HH:mm");
Date date = null;
Date time = null;
try
{
date = form.parse(myTimestamp);
time = new Date(myTimestamp);
SimpleDateFormat postFormater = new SimpleDateFormat("MMM dd");
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String newDateStr = postFormater.format(date).toUpperCase();
String newTimeStr = sdf.format(time);
System.out.println("Date : "+newDateStr);
System.out.println("Time : "+newTimeStr);
}
catch (Exception e)
{
e.printStackTrace();
}
Result :
Date : FEB 17
Time : 20:49
GregorianCalendar date;
CharSequence dateForMart = android.text.format.DateFormat.format("yyyy-MM-dd", date);
Toast.makeText(LogmeanActivity.this,dateForMart,Toast.LENGTH_LONG).show();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "07/06/2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Output:
2014/08/06 16:06:54
2014/08/06 16:06:54