Android Question [ASK] How to convert Date To EPOCH??

hillzx

Member
Licensed User
Longtime User
Almost give up how to convert date to epoch, i already try this code but the result is not what i expected:

B4X:
Private Sub ConvertDateToEpoch(day As Int,month As Int,year As Int,hour As Int,minute As Int) As Long
   

    DateTime.DateFormat = "MM/dd/yyyy"
    DateTime.TimeFormat= "HH:mm:ss"

   
    Dim dateNow As Long =DateTime.DateParse(month & "/" & day & "/" & year & " " & DateTime.TimeParse(hour & ":" & minute & ":00"))
    Dim dateBegin As Long= DateTime.DateParse("01/01/1970" & " " & DateTime.TimeParse("00:00:00" ))

    Dim result As Long = (dateNow - dateBegin)/1000
     
     
    Return result
   
End Sub

i really appreciate any help, thank you guys
 

Johan Schoeman

Expert
Licensed User
Longtime User
Thank you for your response, unfortunately i don't understand how to implement it to b4a
Something like this (you need to enable the JavaObject library and you must use B4A version >= 4.30):

B4X:
#Region  Project Attributes
    #ApplicationLabel: EPOCH
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
 

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
 
    Dim nativeMe As JavaObject

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    nativeMe.InitializeContext
 
    Dim epoch As Long
    Dim dat As String = "Jun 13 2003 23:11:52.454 UTC"
    epoch = nativeMe.RunMethod("convertDateToEpoch", Array(dat))
    Log(epoch)

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

#If Java

import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParseException;


public static long convertDateToEpoch(String str) throws ParseException {
    SimpleDateFormat df = new SimpleDateFormat("MMM dd yyyy HH:mm:ss.SSS zzz");
    Date date = df.parse(str);
    long epoch = date.getTime();
    BA.Log("" + epoch); // 1055545912454
    return epoch;
} 



#End If

Browse the web for SimpleDateFormat and then change the format string in the Java code to match the date string that you need to pass.
 
Last edited:
Upvote 0

hillzx

Member
Licensed User
Longtime User
Something like this (you need to enable the JavaObject library and you must use B4A version >= 4.30):

B4X:
#Region  Project Attributes
    #ApplicationLabel: EPOCH
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.


End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim nativeMe As JavaObject

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    nativeMe.InitializeContext

    Dim epoch As Long
    Dim dat As String = "Jun 13 2003 23:11:52.454 UTC"
    epoch = nativeMe.RunMethod("convertDateToEpoch", Array(dat))
    Log(epoch)

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

#If Java

import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParseException;


public static long convertDateToEpoch(String str) throws ParseException {
    SimpleDateFormat df = new SimpleDateFormat("MMM dd yyyy HH:mm:ss.SSS zzz");
    Date date = df.parse(str);
    long epoch = date.getTime();
    BA.Log("" + epoch); // 1055545912454
    return epoch;
}



#End If

Browse the web for SimpleDateFormat and then change the format string in the Java code to match the date string that you need to pass.
thank you , i will try it
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
The code Johan gave you works well, but if you want to use code without in line java and without the JavaObject lib, you can use this code:
B4X:
Dim MyOrigdate As String =DateTime.DateFormat 'store orig format
DateTime.DateFormat ="MMM dd yyyy HH:mm:ss.SSS zzz"
Dim epoch As Long =DateTime.DateParse("Jun 13 2003 23:11:52.454 UTC")
Log(epoch)  'displays: 1055545912454
DateTime.DateFormat=MyOrigdate  'go back to orig format
 
Upvote 0
Top