This question already has answers here:
How to format date and time in Android?
(26 answers)
Closed 3 years ago.
I was building an android app and i was using Date class in my project to get the current date. I formatted the date with simpledateformatter and displayed it like dd-mm-yyyy (i.e. day month year) .
Now i also want to get the time in format of hh:MM:ss a (hours minutes seconds AM/PM)
As i was using date's instance i saw that it displays date and time also ( in default format). So i tried to fetch time from the date's instance.(let's say d is date class instance). I also found getTime() method of date class and performed d.getTime() but it returned me a long (which is duration from some fixed time from past to current time). Now i want time in desired format but this getTime() method is giving me long.
May you provide me some way on how to process this long value to get the desired format of time out of it. For example , d.getTime() return me some value( say 11233) and i want in format like this (11:33:22).
You can make that
private final String DATE_FORMAT = "dd.MM.yyyy HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
Date got = sdf.parse(date);
It returns Date with time to you
Use this snippet to get the date and time both.
public String currentDateTime() {
Calendar c = Calendar.getInstance();
SimpleDateFormat dateformat = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss aa"); //it will give you the date in the formate that is given in the image
String datetime = dateformat.format(c.getTime()); // it will give you the date
return datetime;
}
Note: Take a look in the image .
Date().getTime() is providing you the timestamp
Change the format to your requirement like mm:hh:ss a
Kotlin
fun getDateTime():String {
val inputFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault())
val date = Date()
return inputFormat.format(date.time)
}
JAVA
private String getDateTime(){
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
return format.format(new Date().getTime());
}
Related
This question already has answers here:
Conversion from 12 hours time to 24 hours time in java
(17 answers)
Closed 6 years ago.
I am storing time in the database in this format: "hh:mm:ss" (example- 09:30:00) and then retrieving and trying to show it to users in this format: "hh:mm AM/PM" (example- 09:30 AM).
I'm using below code for converting it:
DateFormat currentTime = new SimpleDateFormat("hh:mm a");
String startTimeInSF = currentTime.format(startTime);
String endTimeInSF = currentTime.format(endTime);
where startTime and endTime is in hh:mm:ss format, but the above code is producing this error: java.lang.IllegalArgumentException: Bad class: class java.lang.String.
Please let me know how can I successfully convert the time from hh:mm:ss to hh:mm AM/PM?
I think you should parse your "hh:mm:ss" time into a Date Object, then use formatter to format it to "hh:mm a".
Like this :
DateFormat format1 = new SimpleDateFormat("hh:mm:ss");
try {
Date date = format1.parse("01:11:22");
SimpleDateFormat format2 = new SimpleDateFormat("hh:mm a");
String result = format2.format(date);
return result;
} catch (ParseException e) {
e.printStackTrace();
}
I believe you're looking for the parse(String source) method. The format() methods take in a Date object and output a String representation of the object. the parse methods take in a String object and converts it to a Date object.
To do what you want, you'll need to have a DateFormat with hh:mm:ss, convert the database String to a Date using parse, and then use your existing DateFormat and use format on the Date object to get the output String to display to your user.
http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html
You need to change your code something like this, format function will not work directly on String object that is root cause of your exception.
DateFormat inputFormatter1 = new SimpleDateFormat"HH:mm:ss");
Date date1 = inputFormatter1.parse("22:10:11");
DateFormat outputFormatter1 = new SimpleDateFormat("hh:mm a");
String output1 = outputFormatter1.format(date1); //
Out will be 10:10 pm
This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Closed 6 years ago.
In my Android app I have a string like this, String date = "2016-09-24T06:24:01Z";
I use this code to turn it into a nicer looking date format:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
dateFormat.setTimeZone(TimeZone.getDefault());
DateFormat formatted = new SimpleDateFormat(format);
Date result = dateFormat.parse(date);
dateString = formatted.format(result);
However it's not applying the timezone. I've tried setting it on both dateFormat and formatted and no matter what I do it still comes back with 6:24 AM.
Shouldn't TimeZone.getDefault() be looking at the timezone on the device running the app and adjusting the time accordingly?
As your are using java.util.date which have no Time Zone. It represent UTC/GMT no Time Zone offset. See below thing.
so change this line
dateFormat.setTimeZone(TimeZone.getDefault());
to this
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
or this
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
If you know the current time zone:
TimeZone tzone = TimeZone.getTimeZone("America/Los_Angeles");
tzone.setDefault(tzone);
If you do not know the current timezone:
Calendar cal = Calendar.getInstance();
long milliDiff = cal.get(Calendar.ZONE_OFFSET);
// Got local offset, now loop through available timezone id(s).
String [] ids = TimeZone.getAvailableIDs();
String name = null;
for (String id : ids) {
TimeZone tz = TimeZone.getTimeZone(id);
if (tz.getRawOffset() == milliDiff) {
// Found a match.
name = id;
break;
}
}
TimeZone tzone = TimeZone.getTimeZone(name);
tzone.setDefault(tzone);
I am working the Talend studio tool for data migration. Now I want to set the Current DateTime in the Date field.
I get the DateTime from this code TalendDate.getDate("yyyy-MM-dd HH:mm:ss") but it returns String type data. But I need Date type to insert.Is there any String to date (Sample insert is like this:1999-12-13 16:14:48) conversion is in the Talend Studio.
You can use routine function TalendDate.parseDate to convert a String to a Date.
TalendDate.parseDate("yyyy-MM-dd HH:mm:ss", yourStringData);
If you want the current datetime:
TalendDate.parseDate("yyyy-MM-dd HH:mm:ss", TalendDate.getDate("yyyy-MM-dd HH:mm:ss"));
But this makes no sense. Parsedate function is prepared to receive a string and convert it to a Date object. Date objects have it's own format, so you don't have to care how is stored, you need to change the Date format in the moment you show it, but not when you store it:
// this will produce a correct Date Object to store in your Date field
Date currentDate = TalendDate.getCurrentDate();
When you need to show/print it use SimpleDateFormat for example if you want to show 2015-07-05 16:00:00 you must do like this:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss);
System.out.println("My date formatted is: " + sdf.format(currentDate ));
its very simple with the use of DateFormat in java
public static void convert(String inputDate) throws ParseException {
DateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
Date d = format.parse(inputDate); // example 1999-12-13 16:14:48
System.out.println(d);
}
I have a DateTime object DT which stores current time. When I print DT, I want it to only print the time part, ie HH-MM-SS (H = hours, M = minutes, S = seconds) and ignore the date part.
How can I do this ? For that matter, is it even possible to create a date time object which will only contain HH-MM-SS and nothing related to date ? If that is true, then I can simply print it instead of extracting the HH-MM-SS part.
Thanks.
If you only want the time, you should use a LocalTime instead of a DateTime. You can use DateTime.toLocalTime() to get the time part of an existing DateTime.
If you actually want to keep the DateTime but only reveal the time part when formatting, you can create a DateTimeFormatter with a pattern which only includes the time parts, but I'd usually consider this a design smell.
You can use Java date formatter which is in java.util.Date package.
Like :
Date todaysDate = new java.util.Date();
1. // Formatting date into yyyy-MM-dd HH:mm:ss e.g 2008-10-10 11:21:10
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = formatter.format(todaysDate);
2. // Formatting date into yyyy-MM-dd e.g 2008-10-10
formatter = new SimpleDateFormat("yyyy-MM-dd");
formattedDate = formatter.format(todaysDate);
3. // Formatting date into MM/dd/yyyy e.g 10/10/2008
formatter = new SimpleDateFormat("MM/dd/yyyy");
formattedDate = formatter.format(todaysDate);
With Java you can do it like this
Date obj = new Date() ;
System.out.println(new SimpleDateFormat("hh:mm:ss").format(obj)) ;
but it could be an expensive call.
But jodatime gives LocalTime which you can try out.
I have a date that I get from a server formatted in EST like this
05/07/2012 16:55:55 goes month/day/year then time
if the phone is not in EST how can I convert it to the timezone the phone is in?
it would be not problem if I got the time in milliseconds but I dont
EDIT:
ok now the time is not correct when formatting
String sTOC = oNewSTMsg.getAttribute("TOC").toString();
String timezoneID = TimeZone.getDefault().getID();
DateFormat format = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("EST"));
String newtimezoneID = TimeZone.getDefault().getID();
Date timestamp = null;
try{
timestamp = format.parse(sTOC);
format.setTimeZone(TimeZone.getDefault());
timezoneID = format.format(timestamp);
}catch(ParseException e){
}
I convert it to "EST" then format that time to the default TimeZone but the time is always off by an hour, not sure why?
Use the following code to get a UNIX timestamp:
String serverResp = "05/07/2012 16:55:55";
DateFormat format = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
Date date = format.parse(serverResp);
Now you have the timestamp, which you know how to use.
Here's another question which covers conversion, in case you are curious: Android Convert Central Time to Local Time
Use the DateFormat class to parse the String into a Date. See the introduction to the API document here... http://docs.oracle.com/javase/1.5.0/docs/api/java/text/DateFormat.html
You can then create a Calendar for the Date...
Calendar cal = Calendar.getInstance().setTime(date);
And then you can change the timezone on the Calendar to a different timezone using setTimezone(). Or just get the time in milliseconds, using getTimeInMillis()
Using the Calendar, Date, and DateFormat classes should put you in the right direction.
See the Calendar documentation here... http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html