I have some examples:
case1. val date1 = new DateTime("2015-08-03T04:59:00.000")
output: new DateTime("2015-08-03T04:00:00.000")
case2. val date2 = new DateTime("2015-08-03T04:15:00.000")
output: new DateTime("2015-08-03T04:00:00.000")
means for any datetime if the time is more the 1 minute output should be start of hour. Example for day: datetime.withTimeAtStartOfDay.
I'm assuming that you are using the joda time DateTime. If you are, then you can use the following method
def dateTimeAtStartOfHour(s: String) = {
new DateTime(s)
.withMinuteOfHour(0)
.withSecondOfMinute(0)
.withMillisOfSecond(0)
}
val date1 = dateTimeAtStartOfHour("2015-08-03T04:59:00.000")
val date2 = dateTimeAtStartOfHour("2015-08-03T04:15:00.000")
val date3 = dateTimeAtStartOfHour("2015-08-03T04:59:13.000")
Output is
date1: org.joda.time.DateTime = 2015-08-03T04:00:00.000-04:00
date2: org.joda.time.DateTime = 2015-08-03T04:00:00.000-04:00
date3: org.joda.time.DateTime = 2015-08-03T04:00:00.000-04:00
You need to use "truncate" analogue, which is called roundFloorCopy in joda:
https://stackoverflow.com/a/8510936/1349366
Related
could you help me?
I get a string of type 01092020, how could I convert to date type in 01/09 format, just month and year?
You should use DateTimeFormatter as follows:
fun main(args: Array<String>) {
val dateString = "01092020"
val readingFormatter = DateTimeFormatter.ofPattern("ddMMyyyy")
val date = LocalDate.parse(dateString, readingFormatter)
val writingFormatter = DateTimeFormatter.ofPattern("MM/yy")
val formattedDate = date.format(writingFormatter)
print(formattedDate)
}
You can substring your string and break into month and year e.g DDMMYYYY
val dateStr = "01092020"
val dd = dateStr.substring(0..1)
val mm = dateStr.substring(2..3)
val yyyy = dateStr.substring(4 until dateStr.length)
val DDYY = "$dd/$yyyy"
val mmYYYY = "$mm/$yyyy"
I created a datepicker and users are able to pick a date from it, in another textview i want to show the date of one exact month later. (E.g. User chooses 25th of February, the view will show 25th of March)
val simpleDateFormat = SimpleDateFormat("dd/MM/yyyy", Locale.getDefault())
val getDate :Calendar = Calendar.getInstance()
val datepicker = DatePickerDialog(this,android.R.style.Theme_Holo_Light_Dialog_MinWidth,DatePickerDialog.OnDateSetListener
{ datePicker, i, i2, i3 ->
val selectDate :Calendar = Calendar.getInstance()
selectDate.set(Calendar.YEAR,i)
selectDate.set(Calendar.MONTH,i2)
selectDate.set(Calendar.DAY_OF_MONTH,i3)
val date :String = simpleDateFormat.format(selectDate.time)
sulusText.setText(date)
},getDate.get(Calendar.YEAR),getDate.get(Calendar.MONTH),getDate.get(Calendar.DAY_OF_MONTH))
datepicker.show()
}
}
So here user can choose the date with sulustext and in another view i'd like show the date of one month later.
LocalDate would be a lot easier.
You can create a LocalDate based on the date picker, and then use a built in method to add one month
import java.time.LocalDate
val today = LocalDate.now()
data class DatePicker(val day: Int, val month: Int)
val datePicker = DatePicker(15, 2)
val baseMonth = LocalDate.of(today.year, datePicker.month, datePicker.day)
// add one month
val nextMonth = LocalDate.from(baseMonth).plusMonths(1)
Note: The data class in the example is for demonstration purposes only. You could replace it with your actual date picker implementation.
I've written function that gets a current day of month (integer) and return new day of month incremented by needed value. In my case it's a week. If you have a question please ask me.
#SuppressLint("SimpleDateFormat")
private fun getNewDayOfMonth(day: Int): String {
val calendar = Calendar.getInstance()
var dayOfMonth = day.toString()
val simpleDateFormat = SimpleDateFormat("dd")
calendar.time = simpleDateFormat.parse(dayOfMonth) as Date
calendar.add(Calendar.DATE, 7)
dayOfMonth = simpleDateFormat.format(calendar.time)
return dayOfMonth
}
I have a date in the form of a string like below
"08/08/2017 11:43"
I was trying to subtract a day from the above date string so the final output would be "07/08/2017 11:43"
I tried it with the below code
val x = "18/8/2017 11:43"
val formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm");
val dt = formatter.parseDateTime(x);
println(dt.minusDays(1))
But, the output that I got is 2017-08-17T11:43:00.000-07:00
Is there a better way of doing this?
You need the formatter for both parsing and formatting for output.
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
val x = "18/8/2017 11:43"
val formatter = DateTimeFormatter.ofPattern("d/M/yyyy HH:mm")
val dt = LocalDateTime.parse(x, formatter)
val res = dt.minusDays(1).format(formatter) //res: String = 17/8/2017 11:43
I have a string of time I need to parse that's currently in a 12 hour clock format. Some examples are the following:
11:30 PM
07:00 AM
These times are currently in eastern time zone. What's the best way to convert those strings to a string that contains the UTC equivalent?
How about:
import java.text.SimpleDateFormat
val fmtFromLocal = new SimpleDateFormat("hh:mm a z") // z parses time zone
val fmtToGmt = new SimpleDateFormat("hh:mm a")
def toGmt(t: String): String = fmtToGmt.format(fmtFromLocal.parse(s + " EST"))
Unfortunately, this fails when the local timezone is not GMT, as .parse() returns a local time.
Corrected:
import java.text.SimpleDateFormat
import java.util.{Date, TimeZone}
val localTz = TimeZone.getDefault()
val currentOffset = localTz.getOffset(System.currentTimeMillis)
val fmtFromLocal = new SimpleDateFormat("hh:mm a z") // z parses time zone
val fmtToGmt = new SimpleDateFormat("hh:mm a")
def toGmt(t: String): String = {
val time = fmtFromLocal.parse(t).getTime()
val timeUtc = time + currentOffset
fmtToGmt.format(new Date(timeUtc))
}
(Not tested)
Use Joda-Time.
val tz = DateTimeZone.forTimeZone(TimeZone.getTimeZone("EST"))
// You can also use DateTimeZone.forID(), but it requires an id in form
// like 'Europe/Paris', but I don't remember which such ID corresponds to Eastern time
val fmt = DateTimeFormat.forPattern("hh:mm a")
val fmtIn = fmt.withZone(tz)
val fmtOut = fmt.withZone(DateTimeZone.UTC)
val strIn = "11:30 PM"
val strOut = fmtOut.print(fmtIn.parseDateTime(strIn))
Easy and pretty straightforward. I'm getting 04:40 AM for 11:30 PM, which seems to be correct, and I'm in UTC+4 zone, so it this method works independently on local time zone.
This is my code in scala
var s:String ="22/08/2013"
var simpleDateFormat:SimpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
var date:Date = simpleDateFormat.parse(s);
println(date)
the date is not change. the format of date same as 22/08/2013 there is no change to 2013/08/22
How to change the format dd/mm/yyyy to yyyy/mm/dd in scala
You need to define a SimpleDateFormat which first parses your string and get Date from it. And then convert it to whatever format.
var s:String ="22/08/2013"
var simpleDateFormat:SimpleDateFormat = new SimpleDateFormat("dd/mm/yyyy");
var date:Date = simpleDateFormat.parse(s);
val ans = new SimpleDateFormat("yyyy/mm/dd").format(date)
println(ans)
I tried this it works for me.
val s: String = "22/08/2013"
val simpleDateFormat: SimpleDateFormat = new SimpleDateFormat("dd/mm/yyyy")
val date = simpleDateFormat.parse(s)
val df = new SimpleDateFormat("yyyy/mm/dd")
println(df.format(date))
There's a subtle error in John Vishal's answer:
mm defines 08 as minutes, not as month. Use MM instead.
Corrected code:
val s = "2013_08_14"
val simpleDateFormat: SimpleDateFormat = new SimpleDateFormat("yyyy_MM_dd")
val date = simpleDateFormat.parse(s)
val df = new SimpleDateFormat("dd-MMM-yyyy")
println(df.format(date))