I have two strings:
String date = "2011-11-11"
String time="11:00 PM"
i want to merge this date and time and convert them into a long, similar to System.currentTimeMillis().
try this it is working fine
String inputDate=date+" "+time ;;
long parsedDate = HttpDateParser.parse(inputDate);//inputDate should 2011-12-11 11:10:00 PM" formate
System.out.println("========================="+parsedDate);
Date date=new Date(parsedDate);
SimpleDateFormat date1=new SimpleDateFormat("yyyy-mm-dd hh:mm aa");
String opdate=date1.format(date);
System.out.println("========================="+opdate);
Use SimpleDateFormat and parse the String into a Date.
When you have Date you can get .getTime() what's a long
http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
NOT TESTED!!
String date = "2011-11-11";
String time = "11:00 PM";
String toParse = date + " " + time;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm aa");
try {
Date parse = sdf.parse(toParse);
parse.getTime();
} catch (ParseException ex) {
}
String dateTime = "2011-11-11 " + time;
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd-MMM-yy HH:MM");
date = (Date)formatter.parse(dateTime );
long time = date.getTime();
I found this SO post in the same lines.
String date =date+time ;
SimpleDateFormat fmt = new SimpleDateFormat("MM-dd-yyyy HH:mm");
Date myDate = fmt.parse(date);
System.out.println(myDate); //Mon Jun 04 07:05:00 EDT 2007
long timestamp = myDate.getTime();
Related
I have a DateFormatter that outputs the date in the format "2:00pm, Thu 18 Oct":
static func scheduleFormatter() -> DateFormatter {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.amSymbol = "am"
dateFormatter.pmSymbol = "pm"
dateFormatter.dateFormat = "h:mma, EE dd MMM"
return dateFormatter
}
I need to add in the local time zone abbreviation of the user so that the string appears like "2:00 pm GMT, Thu 18 Oct". How can I do that?
Look at the Unicode Date Formatting Patterns:
Probably you want the pattern v:
The short generic non-location format. Where that is unavailable, falls back to the generic location format ("VVVV"), then the short localized GMT format as the final fallback.
or z:
The short specific non-location format. Where that is unavailable, falls back to the short localized GMT format ("O").
Note that time zone names are connected to language and setting posix locale will probably break that.
You could also ask for the abbreviation directly and include it in your format explicitly:
"h:mma, '\(TimeZone.current.abbreviation() ?? "")' EE dd MMM"
To display the time zone you can simply add "zzz" in your dateFormat string. For example: "h:mma zzz, EE dd MMM"
public static Date getCurrentTimeStampDate(){
String currentDate = new SimpleDateFormat(""yyyy:MM:dd HH:mm:ss").format(Calendar.getInstance(TimeZone.getTimeZone("GMT")).getTime());
SimpleDateFormat formater = new SimpleDateFormat(""yyyy:MM:dd HH:mm:ss");
try {
return formater.parse(currentDate);
} catch (ParseException e) {
return null;
}
}
i am passing "01/12/2017" in the fromDate.text(textfield), but receiving unexpected output.
let formatter = DateFormatter.init()
formatter.dateFormat = "dd/mm/yyyy"
startDate = formatter.date(from: fromDate.text!)
print("startDate = \(startDate)")
output is : 31/12/2016
The format of date should be dd/MM/yyyy not dd/mm/yyyy. The mm indicates the minutes and MM indicates the month.
And also add the below line in your code
formatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
This line of code set time zone. If you not, then you get 30/11/2017 in output.
The reason behind this is when string date not contain time then formatter assume that it is midnight and you also not given the timezone so it will take current timezone.
It has to be dd/MM/yyyy dateformat. MM in capital.
func convertToString(of dateTo: Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy" //Your New Date format as per requirement change it own
let newDate: String = dateFormatter.string(from: dateTo) //pass Date here
print(newDate) //New formatted Date string
return newDate
}
I want to convert date into above mentioned format. I have used:
DateTime date1 = DateTime.ParseExact(date, "dd/MM/yyyy", null);
But it gives exception as string is not recognized as a valid date time.
Note:date is of a string datatype and it is in dd MMM, yyyy format.
string is not recognized as a valid date time
Because you're trying to parse the date string from this format:
"dd/MM/yyyy"
But, as you state, the date string is in this format:
"dd MMM, yyyy"
ParseExact means just that... exact. Parse the date from the format it's in:
DateTime date1 = DateTime.ParseExact(date, "dd MMM, yyyy", null);
Then you can output that value in any format you like:
date1.ToString("dd/MM/yyyy");
ParseExact takes the source format as second parameter. Try to use DateTime.ParseExact(date, "dd MMM, yyyy", null);.
#David beat me to the answer, but I just want to add that you should use TryParseExact rather than ParseExact. That way, you can recover from potential problems. For example:
if (DateTime.TryParseExact(date, "dd MMM, yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime date2))
{
date2.ToString("dd/MM/yyyy");
}
else
{
// handle date in incorrect format
}
Can yo please try this:
string dateString = "15 Jun, 2017";
DateTime result = DateTime.ParseExact(dateString, "dd MMM, yyyy", null);
// Changing to dd/MM/yyyy
string myDate = result.ToString("dd/MM/yyyy");
I am parsing date from server into a custom format :-
This is the date :- "8/9/2017 3:58:00 AM" but it is not parsing into Date object using this format "MM/dd/yyyy hh:mm:ss a" because obviously the month, day and hour is single digit
As per my knowledge it should parse automatically because Android's DateFormat parses the same date.
This is the code snippet i am using :-
func getDateString(_ dateString:String) -> String{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "M/dd/yyyy hh:mm:ss a"
let date = dateFormatter.date(from: dateString)
dateFormatter.dateFormat = "dd MMM yyyy"
return dateFormatter.string(from: date!)
}
This function is returning null
Your code should work, although you should also add some error-checking.
Try this (it will run in a Playground):
// returns an empty string "" if the date format is invalid
func getDateString(_ dateString:String) -> String {
var strResult = ""
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "M/dd/yyyy hh:mm:ss a"
if let date = dateFormatter.date(from: dateString) {
dateFormatter.dateFormat = "dd MMM yyyy"
strResult = dateFormatter.string(from: date)
}
return strResult
}
let dateString = getDateString("8/9/2017 3:58:00 AM")
print(dateString) // prints "09 Aug 2017"
If you change your "source" string to an invalid date/time - such as changing the month from 8 to 18 or the hour from 3 to 13 - you will see the returned value is an empty string.
I'm new to programming.
How do I convert this String to local time in Swift?
var string = "7:55:26 PM"
Thanks!
this is how you format the string in to NSDate , deploy the following code
var dateFormat : NSDateFormatter = NSDateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd HH:mm:ss"
var string = "2015-03-27 07:55:26"
let date : NSDate = dateFormat.dateFromString(string)!