short or long (date and time)format display

本文介绍了使用SQL进行日期格式转换的方法,包括短日期格式(如2006-09-22)、长日期格式(如2006年09月22日)以及完整的日期加时间格式(如2006-09-22 12:34:56)。通过具体的SQL语句示例,展示了如何利用T-SQL中的函数和命令来实现这些转换。

declare @dt  datetime

set @dt=getdate()

------short date format(yyyy-m-d)

select replace(convert(varchar(10),@dt,120),N'-0','-')

--------long date format(yyyy年mm月dd日)

select stuff(stuff(convert(char(8),@dt,112),5,0,N'年'),8,0,N)-----2006年09月22日
go-----

-------------complete date+time:yyyy-mm-dd hh:mi:ss:mmm

select convert(char(11),getdate(),120)+convert(char(12),getdate(),114)

public class CalendarViewAdapter extends BaseAdapter { private static final String TAG = "MenuSpinnerAdapter"; private final String mButtonNames []; // Text on buttons // Used to define the look of the menu button according to the current view: // Day view: show day of the week + full date underneath // Week view: show the month + year // Month view: show the month + year // Agenda view: show day of the week + full date underneath private int mCurrentMainView; private final LayoutInflater mInflater; // Defines the types of view returned by this spinner private static final int BUTTON_VIEW_TYPE = 0; static final int VIEW_TYPE_NUM = 1; // Increase this if you add more view types public static final int DAY_BUTTON_INDEX = 0; public static final int WEEK_BUTTON_INDEX = 1; public static final int MONTH_BUTTON_INDEX = 2; public static final int AGENDA_BUTTON_INDEX = 3; // The current selected event's time, used to calculate the date and day of the week // for the buttons. private long mMilliTime; private String mTimeZone; private long mTodayJulianDay; private final Context mContext; private final Formatter mFormatter; private final StringBuilder mStringBuilder; private Handler mMidnightHandler = null; // Used to run a time update every midnight private final boolean mShowDate; // Spinner mode indicator (view name or view name with date) // Updates time specific variables (time-zone, today's Julian day). private final Runnable mTimeUpdater = new Runnable() { @Override public void run() { refresh(mContext); } }; public CalendarViewAdapter(Context context, int viewType, boolean showDate) { super(); mMidnightHandler = new Handler(); mCurrentMainView = viewType; mContext = context; mShowDate = showDate; // Initialize mButtonNames = context.getResources().getStringArray(R.array.buttons_list); mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mStringBuilder = new StringBuilder(50); mFormatter = new Formatter(mStringBuilder, Locale.getDefault()); // Sets time specific variables and starts a thread for midnight updates if (showDate) { refresh(context); } } // Sets the time zone and today's Julian day to be used by the adapter. // Also, notify listener on the change and resets the midnight update thread. public void refresh(Context context) { mTimeZone = Utils.getTimeZone(context, mTimeUpdater); Time time = new Time(mTimeZone); long now = System.currentTimeMillis(); time.set(now); mTodayJulianDay = Time.getJulianDay(now, time.gmtoff); notifyDataSetChanged(); setMidnightHandler(); } // Sets a thread to run 1 second after midnight and update the current date // This is used to display correctly the date of yesterday/today/tomorrow private void setMidnightHandler() { mMidnightHandler.removeCallbacks(mTimeUpdater); // Set the time updater to run at 1 second after midnight long now = System.currentTimeMillis(); Time time = new Time(mTimeZone); time.set(now); long runInMillis = (24 * 3600 - time.hour * 3600 - time.minute * 60 - time.second + 1) * 1000; mMidnightHandler.postDelayed(mTimeUpdater, runInMillis); } // Stops the midnight update thread, called by the activity when it is paused. public void onPause() { mMidnightHandler.removeCallbacks(mTimeUpdater); } // Returns the amount of buttons in the menu @Override public int getCount() { return mButtonNames.length; } @Override public Object getItem(int position) { if (position < mButtonNames.length) { return mButtonNames[position]; } return null; } @Override public long getItemId(int position) { // Item ID is its location in the list return position; } @Override public boolean hasStableIds() { return false; } @Override public View getView(int position, View convertView, ViewGroup parent) { View v; if (mShowDate) { // Check if can recycle the view if (convertView == null || ((Integer) convertView.getTag()).intValue() != R.layout.actionbar_pulldown_menu_top_button) { v = mInflater.inflate(R.layout.actionbar_pulldown_menu_top_button, parent, false); // Set the tag to make sure you can recycle it when you get it // as a convert view v.setTag(new Integer(R.layout.actionbar_pulldown_menu_top_button)); } else { v = convertView; } TextView weekDay = (TextView) v.findViewById(R.id.top_button_weekday); TextView date = (TextView) v.findViewById(R.id.top_button_date); switch (mCurrentMainView) { case ViewType.DAY: weekDay.setVisibility(View.VISIBLE); weekDay.setText(buildDayOfWeek()); date.setText(buildFullDate()); break; case ViewType.WEEK: if (Utils.getShowWeekNumber(mContext)) { weekDay.setVisibility(View.VISIBLE); weekDay.setText(buildWeekNum()); } else { weekDay.setVisibility(View.GONE); } date.setText(buildMonthYearDate()); break; case ViewType.MONTH: weekDay.setVisibility(View.GONE); date.setText(buildMonthYearDate()); break; case ViewType.AGENDA: weekDay.setVisibility(View.VISIBLE); weekDay.setText(buildDayOfWeek()); date.setText(buildFullDate()); break; default: v = null; break; } } else { if (convertView == null || ((Integer) convertView.getTag()).intValue() != R.layout.actionbar_pulldown_menu_top_button_no_date) { v = mInflater.inflate( R.layout.actionbar_pulldown_menu_top_button_no_date, parent, false); // Set the tag to make sure you can recycle it when you get it // as a convert view v.setTag(new Integer(R.layout.actionbar_pulldown_menu_top_button_no_date)); } else { v = convertView; } TextView title = (TextView) v; switch (mCurrentMainView) { case ViewType.DAY: title.setText(mButtonNames [DAY_BUTTON_INDEX]); break; case ViewType.WEEK: title.setText(mButtonNames [WEEK_BUTTON_INDEX]); break; case ViewType.MONTH: title.setText(mButtonNames [MONTH_BUTTON_INDEX]); break; case ViewType.AGENDA: title.setText(mButtonNames [AGENDA_BUTTON_INDEX]); break; default: v = null; break; } } return v; } @Override public int getItemViewType(int position) { // Only one kind of view is used return BUTTON_VIEW_TYPE; } @Override public int getViewTypeCount() { return VIEW_TYPE_NUM; } @Override public boolean isEmpty() { return (mButtonNames.length == 0); } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { View v = mInflater.inflate(R.layout.actionbar_pulldown_menu_button, parent, false); TextView viewType = (TextView)v.findViewById(R.id.button_view); TextView date = (TextView)v.findViewById(R.id.button_date); switch (position) { case DAY_BUTTON_INDEX: viewType.setText(mButtonNames [DAY_BUTTON_INDEX]); if (mShowDate) { date.setText(buildMonthDayDate()); } break; case WEEK_BUTTON_INDEX: viewType.setText(mButtonNames [WEEK_BUTTON_INDEX]); if (mShowDate) { date.setText(buildWeekDate()); } break; case MONTH_BUTTON_INDEX: viewType.setText(mButtonNames [MONTH_BUTTON_INDEX]); if (mShowDate) { date.setText(buildMonthDate()); } break; case AGENDA_BUTTON_INDEX: viewType.setText(mButtonNames [AGENDA_BUTTON_INDEX]); if (mShowDate) { date.setText(buildMonthDayDate()); } break; default: v = convertView; break; } return v; } // Updates the current viewType // Used to match the label on the menu button with the calendar view public void setMainView(int viewType) { mCurrentMainView = viewType; notifyDataSetChanged(); } // Update the date that is displayed on buttons // Used when the user selects a new day/week/month to watch public void setTime(long time) { mMilliTime = time; notifyDataSetChanged(); } // Builds a string with the day of the week and the word yesterday/today/tomorrow // before it if applicable. private String buildDayOfWeek() { Time t = new Time(mTimeZone); t.set(mMilliTime); long julianDay = Time.getJulianDay(mMilliTime,t.gmtoff); String dayOfWeek = null; mStringBuilder.setLength(0); if (julianDay == mTodayJulianDay) { dayOfWeek = mContext.getString(R.string.agenda_today, DateUtils.formatDateRange(mContext, mFormatter, mMilliTime, mMilliTime, DateUtils.FORMAT_SHOW_WEEKDAY, mTimeZone).toString()); } else if (julianDay == mTodayJulianDay - 1) { dayOfWeek = mContext.getString(R.string.agenda_yesterday, DateUtils.formatDateRange(mContext, mFormatter, mMilliTime, mMilliTime, DateUtils.FORMAT_SHOW_WEEKDAY, mTimeZone).toString()); } else if (julianDay == mTodayJulianDay + 1) { dayOfWeek = mContext.getString(R.string.agenda_tomorrow, DateUtils.formatDateRange(mContext, mFormatter, mMilliTime, mMilliTime, DateUtils.FORMAT_SHOW_WEEKDAY, mTimeZone).toString()); } else { dayOfWeek = DateUtils.formatDateRange(mContext, mFormatter, mMilliTime, mMilliTime, DateUtils.FORMAT_SHOW_WEEKDAY, mTimeZone).toString(); } return dayOfWeek.toUpperCase(); } // Builds strings with different formats: // Full date: Month,day Year // Month year // Month day // Month // Week: month day-day or month day - month day private String buildFullDate() { mStringBuilder.setLength(0); String date = DateUtils.formatDateRange(mContext, mFormatter, mMilliTime, mMilliTime, DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR, mTimeZone).toString(); return date; } private String buildMonthYearDate() { mStringBuilder.setLength(0); String date = DateUtils.formatDateRange( mContext, mFormatter, mMilliTime, mMilliTime, DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_MONTH_DAY | DateUtils.FORMAT_SHOW_YEAR, mTimeZone).toString(); return date; } private String buildMonthDayDate() { mStringBuilder.setLength(0); String date = DateUtils.formatDateRange(mContext, mFormatter, mMilliTime, mMilliTime, DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR, mTimeZone).toString(); return date; } private String buildMonthDate() { mStringBuilder.setLength(0); String date = DateUtils.formatDateRange( mContext, mFormatter, mMilliTime, mMilliTime, DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_NO_MONTH_DAY, mTimeZone).toString(); return date; } private String buildWeekDate() { // Calculate the start of the week, taking into account the "first day of the week" // setting. Time t = new Time(mTimeZone); t.set(mMilliTime); int firstDayOfWeek = Utils.getFirstDayOfWeek(mContext); int dayOfWeek = t.weekDay; int diff = dayOfWeek - firstDayOfWeek; if (diff != 0) { if (diff < 0) { diff += 7; } t.monthDay -= diff; t.normalize(true /* ignore isDst */); } long weekStartTime = t.toMillis(true); // The end of the week is 6 days after the start of the week long weekEndTime = weekStartTime + DateUtils.WEEK_IN_MILLIS - DateUtils.DAY_IN_MILLIS; // If week start and end is in 2 different months, use short months names Time t1 = new Time(mTimeZone); t.set(weekEndTime); int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR; if (t.month != t1.month) { flags |= DateUtils.FORMAT_ABBREV_MONTH; } mStringBuilder.setLength(0); String date = DateUtils.formatDateRange(mContext, mFormatter, weekStartTime, weekEndTime, flags, mTimeZone).toString(); return date; } private String buildWeekNum() { int week = Utils.getWeekNumberFromTime(mMilliTime, mContext); return mContext.getResources().getQuantityString(R.plurals.weekN, week, week); } }为什么top_button_date的初始值被设为January 2010
08-15
Usage: -h --help Show all help information. Show single help information with option: query/clear/buffer/stats/persist/private/kmsg/flowcontrol/baselevel/domain/combo Querying logs options: No option performs a blocking read and keeps printing. -x --exit Performs a non-blocking read and exits when all logs in buffer are printed. -a <n>, --head=<n> Show n lines logs on head of buffer. -z <n>, --tail=<n> Show n lines logs on tail of buffer. -t <type>, --type=<type> Show specific type/types logs with format: type1,type2,type3 Don't show specific type/types logs with format: ^type1,type2,type3 Type coule be: app/core/init/kmsg/only_prerelease, kmsg can't combine with others. Default types are: app,core,init,only_prerelease. -L <level>, --level=<level> Show specific level/levels logs with format: level1,level2,level3 Don't show specific level/levels logs with format: ^level1,level2,level3 Long and short level string are both accepted Long level string coule be: DEBUG/INFO/WARN/ERROR/FATAL. Short level string coule be: D/I/W/E/F. Default levels are all levels. -D <domain>, --domain=<domain> Show specific domain/domains logs with format: domain1,domain2,doman3 Don't show specific domain/domains logs with format: ^domain1,domain2,doman3 Max domain count is 5. See domain description at the end of this message. -T <tag>, --tag=<tag> Show specific tag/tags logs with format: tag1,tag2,tag3 Don't show specific tag/tags logs with format: ^tag1,tag2,tag3 Max tag count is 10. -P <pid>, --pid=<pid> Show specific pid/pids logs with format: pid1,pid2,pid3 Don't show specific domain/domains logs with format: ^pid1,pid2,pid3 Max pid count is 5. -e <expr>, --regex=<expr> Show the logs which match the regular expression <expr>. -v <format>, --format=<format> Show logs in different formats, options are: color or colour display colorful logs by log level.i.e. DEBUG INFO WARN ERROR FATAL time format options are(single accepted): time display local time, this is default. epoch display the time from 1970/1/1. monotonic display the cpu time from bootup. time accuracy format options are(single accepted): msec display time by millisecond, this is default. usec display time by microsecond. nsec display time by nanosecond. year display the year when -v time is specified. zone display the time zone when -v time is specified. wrap display the log without prefix when a log line is wrapped. long display all metadata fields, separate messages with blank lines. Different types of formats can be combined, such as: -v color -v time -v msec -v year -v zone. -r Remove all logs in hilogd buffer, advanced option: -t <type>, --type=<type> Remove specific type/types logs in buffer with format: type1,type2,type3 Type coule be: app/core/init/kmsg/only_prerelease. Default types are: app,core,only_prerelease -g Query hilogd buffer size, advanced option: -t <type>, --type=<type> Query specific type/types buffer size with format: type1,type2,type3 Type coule be: app/core/init/kmsg/only_prerelease. Default types are: app,core,only_prerelease -G <size>, --buffer-size=<size> Set hilogd buffer size, <size> could be number or number with unit. Unit could be: B/K/M/G which represents Byte/Kilobyte/Megabyte/Gigabyte. <size> range: [64.0K,16.0M]. Advanced option: -t <type>, --type=<type> Set specific type/types log buffer size with format: type1,type2,type3 Type coule be: app/core/init/kmsg/only_prerelease. Default types are: app,core,only_prerelease **It's a persistant configuration** -s, --statistics Query log statistic information. Set param persist.sys.hilog.stats true to enable statistic. Set param persist.sys.hilog.stats.tag true to enable statistic of log tag. -S Clear hilogd statistic information. -w <control>,--write=<control> Log persistance task control, options are: query query tasks informations stop stop all tasks start start one task refresh refresh buffer content to file clear clear /data/log/hilog/hilog*.gz Persistance task is used for saving logs in files. The files are saved in directory: /data/log/hilog/ Advanced options: -f <filename>, --filename=<filename> Set log file name, name should be valid of Linux FS. -l <length>, --length=<length> Set single log file size. <length> could be number or number with unit. Unit could be: B/K/M/G which represents Byte/Kilobyte/Megabyte/Gigabyte. <length> range: [64.0K, 512.0M]. -n <number>, --number<number> Set max log file numbers, log file rotate when files count over this number. <number> range: [2, 1000]. -m <compress algorithm>,--stream=<compress algorithm> Set log file compressed algorithm, options are: none write file with non-compressed logs. zlib write file with zlib compressed logs. -j <jobid>, --jobid<jobid> Start/stop specific task of <jobid>. <jobid> range: [10, 0xffffffff). User can start task with options (t/L/D/T/P/e/v) as if using them when "Query logs" too. **It's a persistant configuration** -p <on/off>, --privacy <on/off> Set HILOG api privacy formatter feature on or off. **It's a temporary configuration, will be lost after reboot** -k <on/off>, --kmsg <on/off> Set hilogd storing kmsg log feature on or off **It's a persistant configuration** -Q <control-type> Set log flow-control feature on or off, options are: pidon process flow control on pidoff process flow control off domainon domain flow control on domainoff domain flow control off domainverifyon enable non whitelist domain domainverifyoff disable non whitelist domain **It's a temporary configuration, will be lost after reboot** -b <loglevel>, --baselevel=<loglevel> Set global loggable level to <loglevel> Long and short level string are both accepted. Long level string coule be: DEBUG/INFO/WARN/ERROR/FATAL/X. Short level string coule be: D/I/W/E/F/X. X means that loggable level is higher than the max level, no log could be printed. Advanced options: -D <domain>, --domain=<domain> Set specific domain loggable level. See domain description at the end of this message. -T <tag>, --tag=<tag> Set specific tag loggable level. The priority is: tag level > domain level > global level. **It's a temporary configuration, will be lost after reboot** --persist Set persist configuration The priority is: tag level > persist tag level > domain level > persist domain level > global level > persist global level. The first layer options can't be used in combination, ILLEGAL expamples: hilog -S -s; hilog -w start -r; hilog -p on -k on -b D Domain description: Log type "core" & "init" & "only_prerelease" are used for OS subsystems, the range is [0xd000000, 0xd0fffff] Log type "app" is used for applications, the range is [0x0, 0xffff] To reduce redundant info when printing logs, only last five hex numbers of domain are printed So if user wants to use -D option to filter OS logs, user should add 0xD0 as prefix to the printed domain: Exapmle: hilog -D 0xD0xxxxx The xxxxx is the domain string printed in logs. Dictionary description: -d <path>, --dictionary=<path> Set elf file path, name should be valid of Linux FS. Rescan the elf file in the system to generate a full data dictionary file
12-24
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值