Android Code Snippet Return the current month as a string

Discussion in 'Code Snippets' started by Peter Simpson, Feb 15, 2015.

  1. Peter Simpson

    Peter Simpson Expert Licensed User

    SubName: Returns the current month as a full string
    Description: The following returns the current month as a full string, thus there's no need to worry about putting the months in a string array to loop through it.

    The following code returns the month as January, February, March, April, May, June, July...
    Code:
    'First store the original date format.
        Dim Ref As Reflector
            Ref.Target = Ref.RunStaticMethod(
    "android.text.format.DateFormat""getDateFormat"Array As Object(Ref.GetContext), Array As String("android.content.Context")) 'Returns default system date pattern
    'Then set the date format and store/print the month
        DateTime.DateFormat = "MMMM"
        
    Log(DateTime.Date(DateTime.Now)) 'February
    'Now restore the original date format.
        DateTime.DateFormat = Ref.RunMethod("toPattern")
        
    Log(DateTime.Date(DateTime.Now)) '15/02/2015 in my case
    Tags: date format, return month
     
    Last edited: Feb 15, 2015
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. Peter Simpson

    Peter Simpson Expert Licensed User

    Yes you're 100% correct, but if like me you don't want to load the DateUtils library(yes reflector is also a library I no I no I no) just to get one simple result like the month in text, then this will do just fine, especially if you're already using the Reflector library for something else. So yes you don't need to do it this way, but just like other things in B4A, there are plenty of ways go get the results you're look for @Erel :)

    If you're not using the Reflector library already, then that's great, but I usually have the reflector library already loaded so I will just utilise that as much as possible instead of just loading more and more libraries no for reasons. But you are correct as I stated earlier.

    Now I'm going to drink a nice cold beer as I've just got back from a quick bike ride :cool:
     
    Last edited: Feb 15, 2015
  4. Erel

    Erel Administrator Staff Member Licensed User

    Enjoy your beer. My point is that handling dates correctly can be complicated and you should always start with DateUtils.

    You do not need to worry about adding another library. DateUtils will add 4kb at most to your APK.
     
    Peter Simpson likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice